 <p>Many answers here discuss the potential overflow issues of using <code>scanf("%s", buf)</code>, but the latest POSIX specification more-or-less resolves this issue by providing an <code>m</code> assignment-allocation character that can be used in format specifiers for <code>c</code>, <code>s</code>, and <code>[</code> formats. This will allow <code>scanf</code> to allocate as much memory as necessary with <code>malloc</code> (so it must be freed later with <code>free</code>).</p> <p>An example of its use:</p> <pre><code>char *buf; scanf("%ms", &amp;buf); // with 'm', scanf expects a pointer to pointer to char. // use buf free(buf); </code></pre> <p>See <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html" rel="nofollow">here</a>. Disadvantages to this approach is that it is a relatively recent addition to the POSIX specification and it is not specified in the C specification at all, so it remains rather unportable for now.</p>