-2

I am facing problem regarding scanf in C. When I run this:

char c;
int a, b;

scanf("%d", &a);
scanf("%c", &c);
scanf("%d", &b);

Then first two scanf were working properly but 3rd one is skipped completely. I searched different posts in this forum about this problem and found a lot of information, but want to know something else.

I already found that the easiest solution would be:

scanf("%d %c %d", &a, &c, &b);

And another solution could be using:

getchar();

And I also found that the reason behind my problem is it writes an extra new line character \n to the buffer, that's why the 3rd one was skipped. But for further researching I found that when I use another scanf of char type after 2nd scanf then it works. That means, in my case the problem occurs if I take input of any integer type after char type. Again I have seen many others were problem having opposite case, they couldn't take input of char after integer. Now I want to be clarified about the exact schemes those are supported in C for scanf that is when I will face similar problems and why char can be scanned after char but integer can't. Thanks to all.

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • 4
    I don;t think you have the problem the way you say it..... the other one which the others are having is the actual problem. – Sourav Ghosh Jun 15 '16 at 12:48
  • 1
    scanf is never skipped. To find out what the result of your scanf was, check its return value. – M.M Jun 15 '16 at 12:51
  • To get help with a specific piece of code, post a [MCVE](http://stackoverflow.com/help/mcve) and also specify exactly what input you typed. – M.M Jun 15 '16 at 12:52
  • http://stackoverflow.com/questions/20306659/the-program-doesnt-stop-on-scanfc-ch-line-why – 001 Jun 15 '16 at 12:53

2 Answers2

8

The "%d" format specifier includes skipping leading white-space. The "%c" format doesn't have that, it reads the next character in the input buffer no matter what kind of character it is.

If you want to skip leading white-space using the "%c" format, you need to tell scanf explicitly to do so with a space in the format string, like e.g. " %c".

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

This is the same problem as "not able to take char after an int", with a small twist.

Recall that when you read an int from an input using scanf with %d format specifier, the characters representing the number are consumed, but the separator following it is not. Consider a buffer that looks like this (I use underscores to show spaces):

char     9 8 _ x _ 7 6
         - - - - - - -
position 0 1 2 3 4 5 6
  • Before the first scanf call your buffer is positioned at zero.
  • After the first call your buffer is positioned at 2.
  • After the second call your buffer is positioned at 3, because space is read for your char

Now the problem becomes clear: the third scanf tries to read an int, but the buffer is at x, so the read is not possible.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523