473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\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 ?

Apr 6 '07 #1
2 1961
On Apr 6, 6:59 am, "arnuld" <geek.arn...@gmail.comwrote:
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.
Apr 6 '07 #2
"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@o5g2000hsb.googlegro ups.com...
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

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from http://www.teranews.com

Apr 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
14
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
19
by: arnuld | last post by:
this programme runs without any error but it does not do what i want it to do: ------------- PROGRAMME -------------- /* K&R2, section 1.6 Arrays; Exercise 1-13. STATEMENT: Write a program...
5
by: arnuld | last post by:
this is a programme that counts the "lengths" of each word and then prints that many of stars(*) on the output . it is a modified form of K&R2 exercise 1-13. the programme runs without any...
2
by: arnuld | last post by:
i was not even able to understand how should i proceed tow rite solution for this exercise: "Write a program to print a histogram of the frequencies of different characters in its input." ...
15
by: arnuld | last post by:
STATEMENT: Write the function strindex(s, t), which returns the position of the rightmost occurence of t in s, or -1 if there is none. PURPOSE: this program prints the index of the rightmost...
6
by: arnuld | last post by:
This is the example from section 4.2, page 71 of K&R2: double atof( char s ) { int i, sign; double val, power; for( i = 0; isspace( s ); ++i )
13
by: Richard Heathfield | last post by:
arnuld said: What do you think that line does? For my own part, I can make no sense whatsoever out of it. -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google...
3
by: Javilen | last post by:
At my company we use the Sax Content Handler to parse XML data files. We are running into a problem where the client is some times sending an & with out the accompanying amp; example. They send...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.