How does C treat trailing spaces in scanf? : <p>If a <em>scanf</em> statement has any white space in the format string, like as follows,  </p>

<pre><code>scanf(" %c",&amp;abc);
</code></pre>

<p>then it just skips over an unlimited number of white spaces until it hits a character.<br>
So <strong>\n p</strong> as input would store <em>p</em> in <strong>abc</strong>.  </p>

<p>Using this concept I am not able to predict the output of the following program that I typed.</p>

<pre><code>char echo ;
do {
       scanf ("%c ", &amp;echo);
       printf ("%c\n",echo);
} while (echo != '\n') ;
</code></pre>

<p>Note that there is a <em>trailing space</em> in the scanf statement.  </p>

<p>Upon execution of the code I get the following behaviour.<br>
It asks for a character. <em>I enter C</em><br>
It asks for a character. <em>I enter I</em><br>
It prints C.<br>
It asks for a character. <em>I enter R</em><br>
It prints I.<br>
It asks for a character. <em>I enter C</em><br>
It prints R.  </p>

<p>This goes on forever. If I press newline, then it just skips it.<br>
Why does it ask for two characters in the beginning? Shouldn't it execute the <em>printf</em> statement?<br>
Why is the character of the previous input get printed in the next one?</p>
