Thread: Multiple scanf calls skip printf lying between them

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    6

    Multiple scanf calls skip printf lying between them

    Code:
    scanf("%d", &a);
    printf("A");
    scanf("%d", &b);
    prints "A" after calling scanf two times instead of between the calls (first scan, then print, then scan). Why? I'm using GCC v4.6

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Can you provide the smallest, compileable program that demonstrates this behavior? Also, provide the exact input you give, down to every number, space, when you press enter. All that can matter.

    Note, stdout is line buffered meaning you usually don't see output until a newline is printed. However, most systems flush the output buffers when input is requested.

    What system are you using GCC 4.6 on?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Code:
    int main()
    {
    int a, b;
    scanf("%d", &a);
    printf("A/n");
    scanf("%d", &b);
    }
    The inputs were (in respective order):
    4
    *return*
    5
    *return*
    The CLI displays the following:
    ~$ gcc a.c
    ~$ ./a.out
    4
    5
    A
    ~$
    I'm using GCC 4.6 on Linux Mint 12 and Tiny C Compiler on Windows 7 premium. Both give the same output.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The code you posted does not match the output you see. The newline in your print is wrong, you need a backslash to escape it. Change /n to \n. Thus, if your output was from the code you posted, I should see
    4
    5
    A/n

    Here's what I get on Fedora 15, GCC 4.6.3
    Code:
    $ ./foo
    4
    A/n5
    So I get the behavior you expect. Maybe something to do with the implementations. I don't know much about Mint, or Tiny C on Windows 7.

    Try again and make sure the code you post is the actual code that produces the error.

    It's late here, I'm calling it a night.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Well, I rebooted my Linux and it worked- which means I have offended the tech gods somehow. Really, the same source file wasn't working previously!
    P.S. I mistyped the backslash when typing the code on forum by my S40v3, my modem's not working.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    As anduril said, your newline was not correct

    An interesting thing is that the stdout stream does not have to flush until a newline is found - Just about all new compilers will flush without the new line, but you should do this if your printf does not finish in a new line

    Code:
    int main(void)
    {
      int a, b;
    
      scanf("%d", &a); 
    
      printf("A");
      fflush(stdout);
    
      scanf("%d", &b);
    
      return 0;
    }
    Have a look at Prelude's answer here: fflush(stdout)
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    6
    Ok, now I understand what is happening. If I do this:
    Code:
    #include <stdio.h>
    int main()
    {
    int a,b;
    printf("A");
    scanf("%d",&a);
    printf("B");
    scanf("%d",&b);
    printf("A=%d\nB=%d",a,b);
    }
    it all works perfectly. However, if I change declare b to be a char like this:
    Code:
    #include <stdio.h>
    int main()
    {
    int a;
    char b;
    printf("A");
    scanf("%d",&a);
    printf("B");
    scanf("%c",&b);
    printf("A=%d\nB=%c\n",a,b);
    }
    a whitespace gets stored in b. To prevent this, I added a space in a's scanf:

    Code:
    #include <stdio.h>
    int main()
    {
    int a;
    char b;
    printf("A");
    scanf("%d ",&a);
    printf("B");
    scanf("%c",&b);
    printf("A=%d\nB=%c",a,b);
    }
    and then the forementioned problem starts i.e. I'm given the b's scanf first and then "B" gets printed. Now please tell me what am I doing wrong.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Multiple Calls to Destructor
    By JJFMJR in forum C++ Programming
    Replies: 9
    Last Post: 09-21-2011, 03:42 PM
  2. Replies: 11
    Last Post: 08-29-2011, 01:35 PM
  3. How to skip scanf command?
    By tqnst in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 10:25 AM
  4. Why does it skip a scanf()?
    By homorapian in forum C Programming
    Replies: 3
    Last Post: 10-21-2010, 04:58 AM
  5. for loop that calls multiple functions
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 07:10 AM

Tags for this Thread