Function to scan input to string buffer in C I have written a function that reads standard input, ignores initial whitespace and fills the buffer with that data.
I think that it is a bit large and cumbersome, can you please tell me what can be improved?

int dscan(char *where, int size) {
    int found = 0;
    int i = 0;
    int j = 0;
    for (i = 0; i &lt; size - 1; i++) {
        char c;
        scanf("%c", &amp;c);
        if (c == '\n') {
            where[j] = '\n';
            where[j + 1] = '\0';
            return j;
        }
        if (isspace(c)) {
            if (!found) {
                continue;
            }
        } else {
            found = 1;
        }
        where[j] = c;
        j++;
    }
    return j;
}
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.