Why?

M

mdh

May I ask the following?

while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )
s[i++]=c;

if ( c == '\n')

etc.

<<<

When I initially incorrectly wrote this code, I had written

"if ( c = '\n')" instead of if "( c == '\n')", apparently a common
"newby" error. I understand the difference, but what I do not
understand is this.

The code runs correctly when "....(c=='\n') is written ie the while
loop performs correctly. However, if the "c='\n' snippet is used, the
while loop only runs once. Why does the while loop not complete with
both correct and incorrect code? I would have thought the expression
"while...etc" is complete in of itlself, either way.

I hope this question makes sense. It may well be that the rest of the
code plays a role.
Thanks in advance.
 
B

Ben Pfaff

mdh said:
while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )
s[i++]=c;

if ( c == '\n') [...]
The code runs correctly when "....(c=='\n') is written ie the while
loop performs correctly. However, if the "c='\n' snippet is used, the
while loop only runs once. Why does the while loop not complete with
both correct and incorrect code? I would have thought the expression
"while...etc" is complete in of itlself, either way.

I think that there must be something else going on. Can you post
a complete program that exhibits this behavior?
 
M

mdh

Ben said:
I think that there must be something else going on. Can you post
a complete program that exhibits this behavior?

It's straight out of K&R...a simple "getline" function..


/*****/
int getline( char s[], int j){

int c,i;

i=0;

while ( --j > 0 && (c=getchar()) != EOF && c != '\n' ) /* test to
make sure line submitted is not empty */

s[i++]=c;


if ( c == '\n') /**** changing this to c=n, caused the behavior
mentioned above ****/

s='\0';

return i;
}


/******/
 
M

Mark McIntyre

May I ask the following?


while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )

stops looping when c equals '\n'
if ( c == '\n')

if you write this wrong, you set c equal to '\n'.
The code runs correctly when "....(c=='\n') is written ie the while
loop performs correctly. However, if the "c='\n' snippet is used, the
while loop only runs once.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

Mark McIntyre

Yes...I get that. But should that have any effect on the while loop?

Because somewhere inside the code you skipped with your (etc), there
was a clue that we can't spot.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
M

mdh

Mark said:
Because somewhere inside the code you skipped with your (etc), there
was a clue that we can't spot.


Well, thanks for looking at it. BTW..."3" above does contain the full
code, as you asked. Maybe it was just a quirk of my system at the time,
as I am now having difficulty reproducing it as well.
Thanks again.
 
C

CBFalconer

mdh said:
Well, thanks for looking at it. BTW..."3" above does contain the
full code, as you asked. Maybe it was just a quirk of my system
at the time, as I am now having difficulty reproducing it as well.

What are we supposed to conclude from '"3" above'? This probably
has something to do with the display on google, which has nothing
to do with reality. Google is NOT usenet.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
M

mdh

CBFalconer said:
What are we supposed to conclude from '"3" above'? This probably
has something to do with the display on google, which has nothing
to do with reality. Google is NOT usenet.

Sorry...I did not KNOW that! Indeed it must be google, as there is a
huge 3 next to the third entry.
Man...this is a tough group. But still, I do get wonderful help on
it...so apologize if I do not know every single in and out of usenet.
 
P

Peter Shaggy Haywood

Groovy hepcat mdh was jivin' on 8 Jun 2006 21:16:47 -0700 in
comp.lang.c.
Why?'s a cool scene! Dig it!
May I ask the following?

while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )
s[i++]=c;

if ( c == '\n')

etc.

<<<

When I initially incorrectly wrote this code, I had written

"if ( c = '\n')" instead of if "( c == '\n')", apparently a common
"newby" error. I understand the difference, but what I do not
understand is this.

The code runs correctly when "....(c=='\n') is written ie the while
loop performs correctly. However, if the "c='\n' snippet is used, the
while loop only runs once. Why does the while loop not complete with
both correct and incorrect code? I would have thought the expression
"while...etc" is complete in of itlself, either way.

When you do c = '\n' you're setting c to the value '\n'. Your loop
runs while (among other things) c != '\n'. So it quits after the first
run through and finds that c has been set to '\n'. Simple, no?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
M

mdh

Peter said:
When you do c = '\n' you're setting c to the value '\n'. Your loop
runs while (among other things) c != '\n'. So it quits after the first
run through and finds that c has been set to '\n'. Simple, no?

I think this must be it. However, it still does not quite explain why
the while loop would not first complete execution until a '\n' was
found. I think that in the original, even though I did not think so, I
must have put braces around the statements which included the c='\n'
which would then explain what I saw.

Thanks .....I think it makes sense to me now. An earlier poster did
imply that something else was going on, and I think with hindsight it
must have been this.
 
K

Keith Thompson

mdh said:
Sorry...I did not KNOW that! Indeed it must be google, as there is a
huge 3 next to the third entry.
Man...this is a tough group. But still, I do get wonderful help on
it...so apologize if I do not know every single in and out of usenet.

Generally most of our frustration is directed at Google than at its
users -- but users are sometimes hit by the shrapnel.
 
A

Alessandro [AkiRoss] Re

[cut]
while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )
s[i++]=c;

if ( c == '\n')
something()
[/cut]

When you do c = '\n' you're setting c to the value '\n'. Your loop
runs while (among other things) c != '\n'. So it quits after the first
run through and finds that c has been set to '\n'. Simple, no?

But i don't see any braces there... after the while loop there is a
single instruction which is executed, so executing a c='\n' doesn't
affect the while loop.
To me, that wasn't a so-easy solution as some said above: if was extern
to the loop, so why the loop sould be affected?
 
M

mdh

Keith said:
Generally most of our frustration is directed at Google than at its
users -- but users are sometimes hit by the shrapnel.


I understand. I have to say that this group is quite amazing in its'
willingness to help others...unless the 'hint' of homework is
detected...then it can be amazingly ruthless( and hysterically funny at
the same time !!!!)
 
M

Morris Dovey

mdh (in (e-mail address removed)) said:

| Keith Thompson wrote:
|| Generally most of our frustration is directed at Google than at its
|| users -- but users are sometimes hit by the shrapnel.
|
| I understand. I have to say that this group is quite amazing in its'
| willingness to help others...unless the 'hint' of homework is
| detected...then it can be amazingly ruthless( and hysterically
| funny at the same time !!!!)

It's a lot less amazing if it's _your_ beeper calling you in at 2:30
in the morning to discover that someone who really should know better
has done attempted to modify a variable passed as a parameter,
stupidly abused a pointer, or left a gets() call in production code.

But in the newsgroup context, it's only slightly less humorous. :)
 
S

spibou

mdh said:
I think this must be it. However, it still does not quite explain why
the while loop would not first complete execution until a '\n' was
found. I think that in the original, even though I did not think so, I
must have put braces around the statements which included the c='\n'
which would then explain what I saw.

Even if the c ='\n' was part of the while loop it still doesn't explain
it. The test
for the while loop is
while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )
Isn't && a sequence point ? This means that c=getchar() gets
executed and the value of c gets updated *before* the test c != '\n'
is made. So even if c has the value '\n' when entering the test for
the while loop , it still shouldn't make a difference.
 
M

Mark McIntyre

Isn't && a sequence point ?

Not AFAIK, but it is true that the LHS of the && is evaluated before
the right.
So even if c has the value '\n' when entering the test for
the while loop , it still shouldn't make a difference.

In which case you need to post your EXACT code, not a snippet
containing ... or (etc)

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard Bos

Mark McIntyre said:
Not AFAIK,

Yes, it is. 6.5.10#4.
In which case you need to post your EXACT code, not a snippet
containing ... or (etc)

Well, not necessarily. He needs to whittle down his code to the smallest
compilable program that still exhibits this behaviour, and then post
_that_ exact code.

Richard
 
M

Mark McIntyre

Not AFAIK,

Yes, it is. 6.5.10#4.[/QUOTE]

Thanks for the correction.
Well, not necessarily. He needs to whittle down his code to the smallest
compilable program that still exhibits this behaviour, and then post
_that_ exact code.

Exactly.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
S

seni.yin

mdh said:
May I ask the following?
while ( --j > 0 && (c=getchar()) != EOF && c != '\n' )

when you debug you programme, you should set an breakpoint
on the state "while ( --j > 0 && (c=getchar()) != EOF && c = '\n' )",
then you will find out why while loop only runs once!
 

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

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top