 <p>Use <code>fgets()</code> followed by <code>strtol()</code> or <code>sscanf(..."%d"...)</code>.</p> <p>Robust code needs to handle IO and parsing issues. IMO, these are best done separately.</p> <pre><code>char buf[50]; fgets(buf, sizeof buf, stdin); int n; int end = 0; // use to note end of scanning and catch trailing junk if (sscanf(buf, "%d %n", &amp;n, &amp;end) != 1 || buf[end] != '\0') { printf("must enter integer"); } else { good_input(n); } </code></pre> <hr> <p>Note: </p> <p><code>strtol()</code> is a better approach, but a few more steps are needed. <a href="http://stackoverflow.com/questions/21244511/get-int-value-from-string-c/21244609#21244609">Example</a> </p> <p>Additional error checks include testing the result of <code>fgets()</code> and insuring the range of <code>n</code> is reasonable for the code.</p> <p>Note: </p> <p>Avoid mixing <code>fgets()</code> and <code>scanf()</code> in the same code.<br> { I said <code>scanf()</code> here and not <code>sscanf()</code>. }<br> Recommend not to use <code>scanf()</code> at all.</p>