A
arnuld
on page 29, section 1.9 Character Arrays, i see an example:
/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s = c;
if (c == '\n') {
s = c;
++i;
}
s = '\0';
return i;
}
notice the line: for (i=0; i < lim-1 && (c=getchar())!=EOF && c!
='\n'; ++i)
it means when all of the three conditions:
1.) i < lim - 1
2.) (c = gethcra()) != EOF
3.) c !='\n'
must be true to break the loop. how can all of these 3 are true at one
one time ?
when c = '\n' it can't be equal to EOF, it has only one value, '\n' or
EOF . so it mean, this loop must continue forever
??
when i hit "Ctrl-D" (which is EOF signal on Linux), it breaks out of
the loop. why ?
/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
int c, i;
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
s = c;
if (c == '\n') {
s = c;
++i;
}
s = '\0';
return i;
}
notice the line: for (i=0; i < lim-1 && (c=getchar())!=EOF && c!
='\n'; ++i)
it means when all of the three conditions:
1.) i < lim - 1
2.) (c = gethcra()) != EOF
3.) c !='\n'
must be true to break the loop. how can all of these 3 are true at one
one time ?
when c = '\n' it can't be equal to EOF, it has only one value, '\n' or
EOF . so it mean, this loop must continue forever
??
when i hit "Ctrl-D" (which is EOF signal on Linux), it breaks out of
the loop. why ?