The documentation on scanf says that any "Non-whitespace character" in the format, causes the function to read the next character from the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.
However, if I run:
int x;
while(scanf("\n%d",&x)==1) printf("%d\n",x);
with the following input:
1 2
It prints:
1
2
Given that there's no '\n' preceding any of the two numbers, why does scanf read them? Isn't that against the docs?
"\n%d"
since the%d
format ignores leading whitespace anyway."%d"
is sufficient. – Weather Vane