Does scanf() take '\n' as input leftover from previous scanf()?

c, scanf

Solution

`scanf` leaves the input stream pointing to the `\n`. In your case it doesn't make a difference: each time it's called, `scanf` will move along until it finds the next non-whitespace character. So giving it 10 lines of `name, a, b` as input will work as you expect.

But consider this:

scanf("%d", &a);
fgets(str, 20, stdin);

`fgets` reads until it finds the first newline character, so `str` will just get a value of `\n`, and `fgets` will not read the next line of input.

Problem

In the following C code: ``` char name[20]; int a; int b; for(i=0;i<10;i++) { printf("\nEnter name, a & b: "); scanf("%s %d %d",name,&a,&b); } ``` does `scanf` read in the `'\n'` entered at the end of `scanf()` in 1st iteration, for the 2nd iteration inputs?

Original source