prints "A" after calling scanf two times instead of between the calls (first scan, then print, then scan). Why? I'm using GCC v4.6Code:scanf("%d", &a);
printf("A");
scanf("%d", &b);
Printable View
prints "A" after calling scanf two times instead of between the calls (first scan, then print, then scan). Why? I'm using GCC v4.6Code:scanf("%d", &a);
printf("A");
scanf("%d", &b);
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?
The inputs were (in respective order):Code:int main()
{
int a, b;
scanf("%d", &a);
printf("A/n");
scanf("%d", &b);
}
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.
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
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.Code:$ ./foo
4
A/n5
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.
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.
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
Have a look at Prelude's answer here: http://cboard.cprogramming.com/c-pro...tml#post708567Code:int main(void)
{
int a, b;
scanf("%d", &a);
printf("A");
fflush(stdout);
scanf("%d", &b);
return 0;
}
Ok, now I understand what is happening. If I do this:
it all works perfectly. However, if I change declare b to be a char like 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);
}
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\n",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.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);
}
That is correct, but it's usually done in the
http://cboard.cprogramming.com/c-pro...xpected-2.htmlCode:scanf(" %c", &apple);
Which lead to
FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com