The input of this function is s[] - char array and lim - max possible length of this array.
The function itself is used to determine the length of an inputed char array which is entered in the console.
The question is, what's the main idea of c != '\n' condition in for loop?
I guess it's used to break a loop. It's quite clear. But I can't get how it can be implemented if I don't type \n in my input.
Is that a terminator at the end of an array like \0?
If that's the case why should we use the if (c == '\n') condition after that?
The code:
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;
}