0

Problem space:

How is it that while ((len = _getline(line, MAXLEN)) > 0) would make the i inside _getline(...) increment but not print the result until enter key pressed?

Code Example:

#include <stdio.h>
#include <string.h>

#define MAXLEN 1000

int _getline(char s[], int max)
{
    int c, i, l;

    for (i = 0, l = 0; (c = getchar()) != EOF && c != '\n'; ++i) {

        printf("%d\n", i);

        if (i < max - 1) {
            s[l++] = c;
        }
    }

    if (c == '\n' && l < max - 1)
        s[l++] = c;

    s[l] = '\0';
    return l;
}

int main()
{
    int len;
    char line[MAXLEN];

    while ((len = _getline(line, MAXLEN)) > 0)
        ;

    return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
dud3
  • 409
  • 1
  • 6
  • 16
  • Maybe it's because `c != '\n'` inside `for loop`, otherwise `for loop` would continue executing internally, until `c != '\n'` is not satisfied, at that point the program would move to the next lines? – dud3 Oct 17 '16 at 23:01
  • Why would a custom `getline` keep reading characters after `max` has been reached? – John Coleman Oct 17 '16 at 23:01
  • 1
    At file-level, names starting with underscore are reserved for the implementation. They must not be used by application code. – too honest for this site Oct 17 '16 at 23:08
  • By the way -- keyboard input is buffered. The OS doesn't send anything at all to your program before you hit enter. I'm not sure what you expect to happen, but your code runs as I would expect it to run. – John Coleman Oct 17 '16 at 23:14
  • @JohnColeman Sorry I forgot to check for that, but the code should work the way it is, I was just wondering why wouldn't it `printf()` while the `while ((len = _getline(line, MAXLEN)) > 0)` is calling it. – dud3 Oct 17 '16 at 23:14
  • @JohnColeman I see, I think that answers my question, I was expecting that while the keys are pressed, the while loop would execute `_getline(...)` which in turn would `printf(...)`, but instead the while loop keeps on calling `_getline(...)` and increment `i` once I hit enter then the `printf(...)` is executed. – dud3 Oct 17 '16 at 23:18
  • This might be helpful: http://stackoverflow.com/q/421860/4996248 – John Coleman Oct 17 '16 at 23:22

1 Answers1

1

How is it ... (various stuff does not occur) but not print the result until enter key pressed?

Typical input from stdin is line buffered. @John Coleman

Nothing is available to getchar(), it waits and waits, until the Enter or '\n' is pressed, or the buffer fills (maybe 4k characters), or EOF occurs - then it returns with the first character. Subsequent calls to getchar() may return quickly using the buffered line input.

The buffering behavior is dependent on the implementation. It is not specified by C.

Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • one more silly question, does the `for() loop` on `_getline()` internally loop while we type, or does it simply wait once on `getchar()` until we hit enter and loop over everything. I think I got it, `for loop` simply waits for `getchar()` until enter is pressed? – dud3 Oct 18 '16 at 09:51
  • @dud3 `for` loop is irrelevant to I/O. `getchar()` waits until it has some input from `stdin`, then it returns. Since `stdin` is typically _line buffed_, that means the first `getchar()` call _waits_ until user types `"12\n"` and then returns with `'1'`. Next call, `getchar()` promptly returns `'2'`, Next call, `getchar()` promptly returns `'\n'`, Next call, `getchar()` waits until user types `"34\n"` and then returns `'3'`... – chux - Reinstate Monica Oct 18 '16 at 11:46