don't understand the "&&" in K&R2 example

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 ?
 
S

Stephen Sprunk

arnuld said:
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 ?

No, it means all three conditions must be true for the loop to _continue_.
If any one of them is false, the loop is broken.
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

The code you gave says c!='\n', not c=='\n'. Conditions two and three above
say that the loop breaks when c is either EOF or \n; obviously it can't be
both, but it can be either (or neither).
when i hit "Ctrl-D" (which is EOF signal on Linux), it breaks out of
the loop. why ?

Because in that case (c=getchar())!=EOF becomes false, breaking the loop.

S
 
B

Bill Pursell

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 ?

No, the condition in the for statement means that
all 3 of those conditions must be true in order
for the loop to continue.
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

if c == 'a', then it doesn't equal EOF and it doesn't equal
'\n', so conditions 2 and 3 are true. If c == '\n' then
condition 3 is false, so the loop breaks.

when i hit "Ctrl-D" (which is EOF signal on Linux), it breaks out of
the loop. why ?

Typing ctrl-D doesn't send a signal. At a console,
it either sends an EOF into the pipe, or it flushes
the readline buffer. (In fact, your code doesn't
break out of the loop if ^D is not the first thing entered.
It does if ^D is the first thing you type on a line.)
In the case that ^D is the first thing typed, it
sends an EOF, so c != EOF evaluates to false, and
condition 2 is not met, so the loop terminates.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top