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.