Function to scan input to string buffer in C
You must check the return value of scanf. If you don't, you run into an endless loop when reaching the end of the file. And, even worse, you invoke undefined behavior.

You could split the loop into two: first, skip all whitespace, then read the remaining text.

int dscan(char *buf, int bufsize) {
    int ch;

    while ((ch = getc()) != EOF &amp;&amp; ch != '\n' &amp;&amp; isspace(ch)) {
        continue;
    }

    int idx = 0;
    while (ch != EOF &amp;&amp; ch != '\n' &amp;&amp; idx &lt; bufsize) {
        buf[idx++] = ch;
        ch = getc();
    }

    if (ch == '\n' &amp;&amp; idx &lt; bufsize) {
        buf[idx++] = '\n';
    }
    if (idx &lt; bufsize) {
        buf[idx++] = '\0';
    }

    return idx;
}


I changed several things:


EOF is handled correctly. On EOF, the string is returned, properly null-terminated. (This was not the case in your code.)
Leading whitespace does not count towards the buffer, it is simply ignored, like you said in your description. Your code counted it.
Each call to an I/O function is checked for an error result.
It's easy to strip the newline character from the string by removing the corresponding if sentence. Depending on whether the calling code needs this or not.
If dscan(buf, size) &lt; size, the buffer is a string, i.e. it is properly null-terminated.
Calling isspace does not invoke undefined behavior. In your code, if I enter a negative char, the behavior is undefined. See the &lt;ctype.h&gt; documentation.