 <p>From <a href="http://www.cplusplus.com/reference/cstdio/scanf/" rel="nofollow">here</a>:</p> <blockquote> <p>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).</p> </blockquote> <p>So change </p> <pre><code>scanf(" %d ",&amp;a); </code></pre> <p>to</p> <pre><code>scanf("%d",&amp;a); </code></pre>