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

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.

Leave a Comment