The following is getline implementation in K&R on page 29 ,
# define MAXLINE 1000
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;
}
I don't understand why we need to do "i < lim - 1" here in the for loop. For correct indexing, why doesn't " i < lim " suffice ?
Any help would be much appreciated...