By John

2010-09-19 06:41:27 8 Comments

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("\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?


@Dale Diaz 2015-03-05 00:46:21

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

char input_1;
char input_2;
scanf("%c", &input_1);
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

@rkellerm 2010-09-19 09:21:58

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.

@caf 2010-09-19 09:15:41

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).

@cpx 2012-12-15 17:29:02

How exactly does skipping the whitespace helps with discarding the newline? For example: scanf("\n%c", &achar); would work too.

@caf 2012-12-15 22:29:28

@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).

@codaddict 2010-09-19 06:46:04

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 ) 

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

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?

@Jonathan Leffler 2010-09-19 06:47:55

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.

@codaddict 2010-09-19 06:52:43

@Jonathan: You are right. We would need it after the %d. And yes fgets + sscanf is always better.

@John 2010-09-29 01:49:38

@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.

@Jonathan Leffler 2010-09-19 06:44:34

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.

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] What does "static" mean in C?

  • 2009-02-21 06:47:52
  • David
  • 921627 View
  • 1153 Score
  • 19 Answer
  • Tags:   c syntax static

4 Answered Questions

[SOLVED] What does the ??!??! operator do in C?

  • 2011-10-19 16:56:59
  • Peter Olson
  • 261613 View
  • 2004 Score
  • 4 Answer
  • Tags:   c operators trigraphs

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 805743 View
  • 1249 Score
  • 11 Answer
  • Tags:   c function-pointers

2 Answered Questions

[SOLVED] scanf and wrong input makes output wierd

  • 2015-04-11 08:31:47
  • Magnum
  • 711 View
  • 0 Score
  • 2 Answer
  • Tags:   c scanf

2 Answered Questions

[SOLVED] C scanf() problem

  • 2011-03-23 06:02:39
  • Milk
  • 3464 View
  • 3 Score
  • 2 Answer
  • Tags:   c scanf

2 Answered Questions

[SOLVED] when scanf returns 0 in c and just doesn't work

  • 2015-12-31 23:53:52
  • Nous Sa Smily
  • 3776 View
  • 5 Score
  • 2 Answer
  • Tags:   c scanf

1 Answered Questions

[SOLVED] Printf is returning with wrong numbers inputted from scanf

  • 2014-02-06 17:45:44
  • MichaelKettle
  • 1719 View
  • 3 Score
  • 1 Answer
  • Tags:   c printf scanf

Sponsored Content