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;
}