The following function is to return the length of a line that is entered through keyboard. But its saying that (The C Programming language K & R) it will return the length of the line, or zero if end of file is encountered.
But when I analyzed with my basic knowledge in C at least it is returning the length of the line till EOF
. So when does it returns 0
. Or my understanding is wrong. Can anybody clarify me ?
int getline(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=’\n’; ++i)
s[i] = c;
if (c == ’\n’) {
s[i] = c;
++i;
}
s[i] = ’\0’;
return i;
}