Scanf() leaves the new line char in buffer?

The scanf function removes whitespace automatically before trying to parse things other than characters. The character formats (primarily %c ) are the exception; they don't remove whitespace.

The scanf function removes whitespace automatically before trying to parse things other than characters. The character formats (primarily %c) are the exception; they don't remove whitespace.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I am learning C and have some questions. I know my questions may be a common question but I could not find the answer. 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("%d",&c1); // line 3 printf("Enter other char: "); scanf("%d", &c2); // line 4 printf("Done"); // line 5 system("PAUSE"); return 0; } As I read in the C book, the author say 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, I was wondering this only happen with char data types? Because I did not see this problem with int data types as in line 1, 2, 3.

It is right? Thanks. C scanf link|improve this question edited Mar 9 '11 at 3:01winwaed4,2753939 asked Mar 9 '11 at 2:56ipkiss1,386415 83% accept rate.

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

As I read in the C book, the author say 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, I was wondering this only happen with char data types?

Because I did not see this problem with int data types as in line 1, 2, 3. It is right?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions