Reputation: 85
I would like to know what does the compiler do with the whitespaces, newline character when using scanf("%s"). I know that scanf("%s") cannot read whitespaces and newline character.
For example if I enter
hi question
or
hi
question
scanf("%s") reads it without problems.
Below is the code that I am referring to
#include <stdio.h>
int main () {
char str [2][50];
scanf("%s", str[0]);
scanf("%s", str[1]);
printf("%s\n", str[0]);
printf("%s\n", str[1]);
return 0;
}
Upvotes: 1
Views: 58
Reputation: 1838
scanf documentation online:
Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
Upvotes: 1