By ipkiss

2011-03-09 02:56:02 8 Comments

I have the following program:

int main(int argc, char *argv[])
  int a, b;
  char c1, c2;
  printf("Enter something: ");
  scanf("%d",&a); // line 1
  printf("Enter other something: ");
  scanf("%d", &b); // line 2

  printf("Enter a char: ");
  scanf("%c",&c1); // line 3
  printf("Enter another char: ");
  scanf("%c", &c2); // line 4

  printf("Done"); // line 5


  return 0;

As I read in the C book, the author says that scanf() left a new line character in the buffer, therefore, the program does not stop at line 4 for user to enter the data, rather it stores the new line character in c2 and moves to line 5.

Is that right?

However, does this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3. Is it right?


@Jeremiah Willcock 2011-03-09 02:59:48

The scanf() function skips leading whitespace automatically before trying to parse conversions other than characters. The character formats (primarily %c; also scan sets %[…] — and %n) are the exception; they don't skip whitespace.

Use " %c" with a leading blank to skip optional white space. Do not use a trailing blank in a scanf() format string.

Note that this still doesn't consume any trailing whitespace left in the input stream, not even to the end of a line, so beware of that if also using getchar() or fgets() on the same input stream. We're just getting scanf to skip over whitespace before conversions, like it does for %d and other non-character conversions.

Note that non-whitespace "directives" (to use POSIX scanf terminology) other than conversions, like the literal text in scanf("order = %d", &order); doesn't skip whitespace either. The literal order has to match the next character to be read.

So you probably want " order = %d" there if you want to skip a newline from the previous line but still require a literal match on a fixed string, like this question.

@chux - Reinstate Monica 2015-12-12 03:12:42

%c, %n, %[] are the 3 specified expectations that do not consume leading whitespace.

@Suraj Jain 2017-02-19 07:40:39

@chux So Does in Other Cases, The scanf clears all the whitespaces before in the buffer or ignores them for they input but they are still there?

@chux - Reinstate Monica 2017-02-20 02:27:45

@SurajJain Yes,

@Jonathan Leffler 2017-10-17 05:28:29

See Trailing blank in scanf() format string and scanf() asking twice for input while I expect it to ask only once for a discussion of trailing blanks in format strings. They're a bad idea — astoundingly bad if you expect human interaction and bad for program interaction.

@brandizzi 2019-01-27 23:17:20

Another option (that I got from here) is to read and discard the newline by using the assignment-supression option. To do that, we just put a format to read a character with an asterisk between % and c:

scanf("%d%*c",&a); // line 1
scanf("%c%*c",&c1); // line 3

scanf will then read the next char (that is, the newline) but not assign it to any pointer.

In the end, however, I would second the FAQ's last option:

Or, depending on your requirements, you could also forget about scanf()/getchar(), use fgets() to get a line of text from the user and parse it yourself.

@Jonathan Leffler 2020-01-12 15:53:13

The trouble with this technique is that if the user types a then space then newline, the suppressed character conversion reads the space and still leaves the newline. If the user types supercalifragilisticexpialidocious when you expect a, you've got a lot of extra characters to deal with. You can never tell whether a trailing suppressed conversion succeeds, either — they're not counted in the return from scanf().

@Jiwon 2018-08-03 13:52:13

Use getchar() before calling second scanf().

scanf("%c", &c1);
getchar();  // <== remove newline
scanf("%c", &c2);

@Jonathan Leffler 2018-12-02 03:05:22

This works provided that the user didn't type anything else — trailing blanks, for example. But it isn't as good as a loop that scans to the next newline: int c; while ((c = getchar()) != EOF && c != '\n') ; (written over three lines when not in a comment). It is often sufficient; it is not foolproof (and you have to remember that fools are very clever about crashing things).

@Shweta 2011-03-09 06:02:33

Use scanf(" %c", &c2);. This will solve your problem.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] How to clear input buffer in C?

  • 2011-10-26 02:48:17
  • ipkiss
  • 342276 View
  • 81 Score
  • 11 Answer
  • Tags:   c buffer

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite

2 Answered Questions

[SOLVED] How to get newline sign using scanf?

  • 2016-08-12 11:20:01
  • Farhan Fuad
  • 1035 View
  • 1 Score
  • 2 Answer
  • Tags:   c scanf

4 Answered Questions

[SOLVED] C programming: scanf for char pointer not working

1 Answered Questions

[SOLVED] Program stops at scanf

  • 2016-02-11 23:15:26
  • Nebeski
  • 408 View
  • 1 Score
  • 1 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
  • 3772 View
  • 5 Score
  • 2 Answer
  • Tags:   c scanf

2 Answered Questions

[SOLVED] scanf in c and relation input buffer

  • 2015-10-22 12:47:53
  • knowledge
  • 637 View
  • 6 Score
  • 2 Answer
  • Tags:   c scanf

1 Answered Questions

scanf function in objective-c, float and double

  • 2014-07-14 14:11:05
  • Atimis
  • 2194 View
  • 0 Score
  • 1 Answer
  • Tags:   objective-c scanf

Sponsored Content