26

If I try something such as:

int anint;
char achar;

printf("\nEnter any integer:");
scanf("%d", &anint);
printf("\nEnter any character:");
scanf("%c", &achar);
printf("\nHello\n");
printf("\nThe integer entered is %d\n", anint);
printf("\nThe char entered is %c\n", achar);

It allows entering an integer, then skips the second scanf completely, this is really strange, as when I swap the two (the char scanf first), it works fine. What on earth could be wrong?

sbi
  • 219,715
  • 46
  • 258
  • 445
John
  • 1,110
  • 3
  • 14
  • 28
  • 1
    your pointers have the wrong sizes, anint is a char and achar is an int. don't do this. – Variable Length Coder Sep 19 '10 at 06:44
  • @Variable Length Coder: I'm sorry about that, was paraphrasing a simpler example and mixed to the two up. That mistype is not related to my problem, fixed the example. – John Sep 19 '10 at 06:44

5 Answers5

33

When reading input using scanf, the input is read after the return key is pressed but the newline generated by the return key is not consumed by scanf, which means the next time you read a char from standard input there will be a newline ready to be read.

One way to avoid is to use fgets to read the input as a string and then extract what you want using sscanf as:

char line[MAX];

printf("\nEnter any integer:");
if( fgets(line,MAX,stdin) && sscanf(line,"%d", &anint)!=1 ) 
   anint=0;

printf("\nEnter any character:");
if( fgets(line,MAX,stdin) && sscanf(line,"%c", &achar)!=1 ) 
   achar=0;

Another way to consume the newline would be to scanf("%c%*c",&anint);. The %*c will read the newline from the buffer and discard it.

You might want to read this:

C FAQ : Why does everyone say not to use scanf?

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 1
    You'd need the `%*c` after the `%d` format (too, or instead), wouldn't you? Though even that is not reliable - if the user typed a space or something after the number and before the newline. I think `fgets()` + `sscanf()` is better. – Jonathan Leffler Sep 19 '10 at 06:47
  • 3
    @Jonathan: You are right. We would need it after the `%d`. And yes `fgets + sscanf` is always better. – codaddict Sep 19 '10 at 06:52
  • @codeaddict: Sorry for lengthy accept, but you really did give me some more insight on how the internals work in C. I really appreciate the C FAQ link too, all the better to be wise to teach new people these things, if I ever get to help people out later on. – John Sep 29 '10 at 01:49
  • /*Take char input using scanf after int input using scanf just use fflush(stdin) function */ #include #include void main() { int x; char y; clrscr(); printf(" enter an int "); scanf("%d",&x); fflush(stdin); printf("\n Now enter a char"); scanf("%c",&y); printf("\n X=%d and Y=%c",x,y); getch(); } – Rai Singh Dec 01 '21 at 13:20
21

The other answers are correct - %c does not skip whitespace. The easiest way to make it do so is to place whitespace before the %c:

scanf(" %c", &achar);

(Any whitespace in the format string will make scanf consume all consecutive whitespace).

caf
  • 233,326
  • 40
  • 323
  • 462
  • How exactly does skipping the whitespace helps with discarding the newline? For example: `scanf("\n%c", &achar);` would work too. – cpx Dec 15 '12 at 17:29
  • 2
    @cpx: Because a newline is whitespace. Any whitespace in the format string behaves like any other whitespace, so `\n` is just as good as a space (but one more letter). – caf Dec 15 '12 at 22:29
5

It doesn't skip the second scanf(); the second scanf() reads the newline left behind by the first scanf(). Most format codes skip white space; the %c format does not skip white space.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
3

calling getchar() before scanf will also purge the stored line break. More lightweight but more situational

char input_1;
char input_2;
getchar();
scanf("%c", &input_1);
getchar();
scanf("%c", &input_2);

will flush the line breaks, more useful in consecutive lines of code where you know it's only one queued value and not a string

lennon310
  • 12,503
  • 11
  • 43
  • 61
Dale Diaz
  • 31
  • 1
0

Try also _flushall() after each printf call. . Basically, by default MS’s C++ buffers stream output, and the the flushing causes the output stream to empty.

rkellerm
  • 5,362
  • 8
  • 58
  • 95