If a scanf statement has any white space in the format string, like as follows,
scanf(" %c",&abc);
then it just skips over an unlimited number of white spaces until it hits a character.
So \n p as input would store p in abc.
Using this concept I am not able to predict the output of the following program that I typed.
char echo ;
do {
scanf ("%c ", &echo);
printf ("%c\n",echo);
} while (echo != '\n') ;
Note that there is a trailing space in the scanf statement.
Upon execution of the code I get the following behaviour.
It asks for a character. I enter C
It asks for a character. I enter I
It prints C.
It asks for a character. I enter R
It prints I.
It asks for a character. I enter C
It prints R.
This goes on forever. If I press newline, then it just skips it.
Why does it ask for two characters in the beginning? Shouldn't it execute the printf statement?
Why is the character of the previous input get printed in the next one?