Connecting Tech Pros Worldwide Forums | Help | Site Map

explain this.....

muttaa
Guest
 
Posts: n/a
#1: Mar 20 '06
i've a code snippet below:


for(;0;)

printf("Gud Morning");



when this is run,i get the "gud morning" message printed,eventhough the
condition in the for loop is set to false(0).

Please explain this out or give me an idea of the semantics of 'for'
statement......


santosh
Guest
 
Posts: n/a
#2: Mar 20 '06

re: explain this.....


muttaa wrote:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

In future, please try to provide the smallest _compilable_ source code
exhibiting the problem. The above code is not complete. In any case the
printf() statement will not execute.

void * clvrmnky()
Guest
 
Posts: n/a
#3: Mar 20 '06

re: explain this.....


muttaa wrote:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>[/color]
Not for me. That expression _should_ evaluate to false (well, actually,
"non-zero").
Keith Thompson
Guest
 
Posts: n/a
#4: Mar 20 '06

re: explain this.....


"void * clvrmnky()" <clvrmnky.invalid@hotmail.com.invalid> writes:[color=blue]
> muttaa wrote:[color=green]
>> i've a code snippet below:
>> for(;0;)
>> printf("Gud Morning");
>> when this is run,i get the "gud morning" message printed,eventhough
>> the
>> condition in the for loop is set to false(0).
>>[/color]
> Not for me. That expression _should_ evaluate to false (well,
> actually, "non-zero").[/color]

Non-zero is true; zero is false.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson
Guest
 
Posts: n/a
#5: Mar 20 '06

re: explain this.....


"muttaa" <asgosa.eeegct@gmail.com> writes:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

You can learn about the semantics of the "for" statement in any decent
C textbook.

I have a hunch what the actual problem might be, but I'm not going to
guess. Post a complete compilable program (copy-and-paste; *don't*
re-type) so we don't have to guess.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Vladimir S. Oka
Guest
 
Posts: n/a
#6: Mar 20 '06

re: explain this.....


muttaa opined:
[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough
> the condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

Your compiler is badly broken, or you didn't show your /real/ code.

--
BR, Vladimir

MICRO:
Thinker toys.

Robert Gamble
Guest
 
Posts: n/a
#7: Mar 20 '06

re: explain this.....


muttaa wrote:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).[/color]

No you don't. Why don't you copy/paste the exact code you ran as this
obviously isn't it.

Robert Gamble

Default User
Guest
 
Posts: n/a
#8: Mar 20 '06

re: explain this.....


muttaa wrote:
[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough
> the condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]


That's a question for your C textbook. If you don't have one, we can
recommend one. It's pointless for us to try to teach you C via random
questions on the newsgroup.




Brian
void * clvrmnky()
Guest
 
Posts: n/a
#9: Mar 20 '06

re: explain this.....


Keith Thompson wrote:[color=blue]
> "void * clvrmnky()" <clvrmnky.invalid@hotmail.com.invalid> writes:[color=green]
>> muttaa wrote:[color=darkred]
>>> i've a code snippet below:
>>> for(;0;)
>>> printf("Gud Morning");
>>> when this is run,i get the "gud morning" message printed,eventhough
>>> the
>>> condition in the for loop is set to false(0).
>>>[/color]
>> Not for me. That expression _should_ evaluate to false (well,
>> actually, "non-zero").[/color]
>
> Non-zero is true; zero is false.
>[/color]
Sorry, switched my logic during writing this after reading a synopsis of
the spec for control expressions. My comment still holds: the for loop
should not be entered, and I think this is the expected behaviour.
Micah Cowan
Guest
 
Posts: n/a
#10: Mar 20 '06

re: explain this.....


"muttaa" <asgosa.eeegct@gmail.com> writes:
[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

The above snippet won't compile on its own... you are generally
expected to post the minimum amount of /compilable/ code that
illustrates the problem.

I compiled and ran the following, which printed nothing. Are you
certain you have the right code? Please post the /whole/, minimal
program that is printing the message you are seeing.

Also, note that nothing is required to be output (even ignoring the
for condition) if you don't include a newline character or fflush(stdout).

#include <stdio.h>

int main(void)
{
for (;0;)
printf("Gud morning.\n");
}
Pedro Graca
Guest
 
Posts: n/a
#11: Mar 20 '06

re: explain this.....


muttaa wrote:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");[/color]

When I try this in my compiler it outputs:
error: parse error before "for"

[color=blue]
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

K&R2 p. 13, 18, 60, 224

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
mianaome@gmail.com
Guest
 
Posts: n/a
#12: Mar 21 '06

re: explain this.....


this is an example format of a "for" statement... for(i=0;i<10;i++)

in your code, for(;0;), of course, the 0 code does not necessarily mean
that it would be false.... actually, the statement for(;;) would still
be true and therefore, will run the statements after it....
the second argument in the for statement just checks if your variable
i.... a for loop is just like a while loop......

am i able to answer your question??? :p


Ayon kay muttaa:[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......[/color]

Ben Bacarisse
Guest
 
Posts: n/a
#13: Mar 21 '06

re: explain this.....


On Mon, 20 Mar 2006 20:27:06 -0800, mianaome wrote:[color=blue]
> Ayon kay muttaa:[color=green]
>> i've a code snippet below:
>>
>> for(;0;)
>>
>> printf("Gud Morning");
>>
>> when this is run,i get the "gud morning" message printed,eventhough the
>> condition in the for loop is set to false(0).[/color]
> this is an example format of a "for" statement... for(i=0;i<10;i++)
>
> in your code, for(;0;), of course, the 0 code does not necessarily mean
> that it would be false.... actually, the statement for(;;) would still
> be true and therefore, will run the statements after it....
> the second argument in the for statement just checks if your variable
> i.... a for loop is just like a while loop......
>
> am i able to answer your question??? :p[/color]

No, you have, if anything, added to the confusion. 0 is false: when
used in a boolean context it always means false and never anything else.

A C "for" loop like this (the <En> are expressions and <S1> is a statement):

for (<E1>; <E2>; <E3>) <S1>

is essentially equivalent to:

<E1>;
while (<E2>) {
<S1>
<E3>;
}

(although not exactly equivalent because of "continue" statements.) The
OP's loop is therefore equivalent to:

;
while (0) {
printf("...");
;
}

A while statement with a false condition does not execute its body. In
fact the compiler is entitled to remove all of this code and generate
nothing. Hence the OP's (and others') surprise that anything is output.

[I have repaired your top-posting.]
--
Ben.
Charles M. Reinke
Guest
 
Posts: n/a
#14: Mar 21 '06

re: explain this.....


"muttaa" <asgosa.eeegct@gmail.com> wrote in message
news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...[color=blue]
> i've a code snippet below:
>
>
> for(;0;)
>
> printf("Gud Morning");
>
>
>
> when this is run,i get the "gud morning" message printed,eventhough the
> condition in the for loop is set to false(0).
>
> Please explain this out or give me an idea of the semantics of 'for'
> statement......
>[/color]

Any chance your actual code looked something like this:

#include <stdio.h>

int main(void) {
int i;

{
for(;0;)
i = 1;
printf("Gud morning.\n");
}

return 0;
} /* main */

-Charles


Micah Cowan
Guest
 
Posts: n/a
#15: Mar 21 '06

re: explain this.....


"Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:
[color=blue]
> "muttaa" <asgosa.eeegct@gmail.com> wrote in message
> news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...[color=green]
> > i've a code snippet below:
> >
> >
> > for(;0;)
> >
> > printf("Gud Morning");
> >
> >
> >
> > when this is run,i get the "gud morning" message printed,eventhough the
> > condition in the for loop is set to false(0).
> >
> > Please explain this out or give me an idea of the semantics of 'for'
> > statement......
> >[/color]
>
> Any chance your actual code looked something like this:
>
> #include <stdio.h>
>
> int main(void) {
> int i;
>
> {
> for(;0;)
> i = 1;
> printf("Gud morning.\n");
> }
>
> return 0;
> } /* main */[/color]

Would it make a difference? The printf() would still never execute, as
the 0 condition would result in the for's body never being executed,
and in any case, the value of i is never used anywhere.
void * clvrmnky()
Guest
 
Posts: n/a
#16: Mar 21 '06

re: explain this.....


Micah Cowan wrote:[color=blue]
> "Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:
>[color=green]
>> "muttaa" <asgosa.eeegct@gmail.com> wrote in message
>> news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...[color=darkred]
>>> i've a code snippet below:
>>>
>>>
>>> for(;0;)
>>>
>>> printf("Gud Morning");
>>>
>>>
>>>
>>> when this is run,i get the "gud morning" message printed,eventhough the
>>> condition in the for loop is set to false(0).
>>>
>>> Please explain this out or give me an idea of the semantics of 'for'
>>> statement......
>>>[/color]
>> Any chance your actual code looked something like this:
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> int i;
>>
>> {
>> for(;0;)
>> i = 1;
>> printf("Gud morning.\n");
>> }
>>
>> return 0;
>> } /* main */[/color]
>
> Would it make a difference? The printf() would still never execute, as
> the 0 condition would result in the for's body never being executed,
> and in any case, the value of i is never used anywhere.[/color]

Read it again and note what those curly braces delimit. Put the curly
braces around the code the way the compiler might see it.

This discussion is not about correct code (since we have not seen any)
but about the specifics of the control expression in a for() statement.
That "i = 1" is there to illustrate a point and common mistake by novices.
Keith Thompson
Guest
 
Posts: n/a
#17: Mar 21 '06

re: explain this.....


Micah Cowan <micah@cowan.name> writes:[color=blue]
> "Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:[color=green]
>> "muttaa" <asgosa.eeegct@gmail.com> wrote in message
>> news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...[color=darkred]
>> > i've a code snippet below:
>> >
>> >
>> > for(;0;)
>> >
>> > printf("Gud Morning");
>> >
>> >
>> >
>> > when this is run,i get the "gud morning" message printed,eventhough the
>> > condition in the for loop is set to false(0).
>> >
>> > Please explain this out or give me an idea of the semantics of 'for'
>> > statement......
>> >[/color]
>>
>> Any chance your actual code looked something like this:
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> int i;
>>
>> {
>> for(;0;)
>> i = 1;
>> printf("Gud morning.\n");
>> }
>>
>> return 0;
>> } /* main */[/color]
>
> Would it make a difference? The printf() would still never execute, as
> the 0 condition would result in the for's body never being executed,
> and in any case, the value of i is never used anywhere.[/color]

Take a closer look. The body of the for loop is the single statement
"i = 1;"; the printf is executed unconditionally *after* the for loop.
The braces surrounding the whole thing are misleading.

It turns out that wasn't the problem (the OP's ancient compiler was
actually buggy), but it was a reasonable guess. (My own guess was a
trailing semicolon on the "for" line, but that wasn't it either.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Stephen Dedalus
Guest
 
Posts: n/a
#18: Mar 21 '06

re: explain this.....



Micah Cowan wrote:[color=blue]
> "Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:
>[color=green]
> > "muttaa" <asgosa.eeegct@gmail.com> wrote in message
> > news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...[color=darkred]
> > > i've a code snippet below:
> > >
> > >
> > > for(;0;)
> > >
> > > printf("Gud Morning");
> > >
> > >
> > >
> > > when this is run,i get the "gud morning" message printed,eventhough the
> > > condition in the for loop is set to false(0).
> > >
> > > Please explain this out or give me an idea of the semantics of 'for'
> > > statement......
> > >[/color]
> >
> > Any chance your actual code looked something like this:
> >
> > #include <stdio.h>
> >
> > int main(void) {
> > int i;
> >
> > {
> > for(;0;)
> > i = 1;
> > printf("Gud morning.\n");
> > }
> >
> > return 0;
> > } /* main */[/color]
>
> Would it make a difference? The printf() would still never execute, as
> the 0 condition would result in the for's body never being executed,
> and in any case, the value of i is never used anywhere.[/color]

Read more carefully. "i" never gets set to 1, but printf certainly
executes.

Flash Gordon
Guest
 
Posts: n/a
#19: Mar 21 '06

re: explain this.....


Micah Cowan wrote:[color=blue]
> "Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:[/color]

<snip>
[color=blue][color=green]
>> Any chance your actual code looked something like this:
>>
>> #include <stdio.h>
>>
>> int main(void) {
>> int i;
>>
>> {
>> for(;0;)
>> i = 1;
>> printf("Gud morning.\n");
>> }
>>
>> return 0;
>> } /* main */[/color]
>
> Would it make a difference? The printf() would still never execute, as
> the 0 condition would result in the for's body never being executed,
> and in any case, the value of i is never used anywhere.[/color]

Look at the positioning of the braces a bit more carefully...

I'm sure Charles made the mistake deliberately in this case.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
CBFalconer
Guest
 
Posts: n/a
#20: Mar 21 '06

re: explain this.....


"Charles M. Reinke" wrote:[color=blue]
> "muttaa" <asgosa.eeegct@gmail.com> wrote in message
>[color=green]
>> i've a code snippet below:
>>
>> for(;0;)
>> printf("Gud Morning");
>>
>> when this is run,i get the "gud morning" message printed,even
>> though the condition in the for loop is set to false(0).
>>
>> Please explain this out or give me an idea of the semantics of
>> 'for' statement......[/color]
>
> Any chance your actual code looked something like this:
>
> #include <stdio.h>
>
> int main(void) {
> int i;
>
> {
> for(;0;)
> i = 1;
> printf("Gud morning.\n");
> }
>
> return 0;
> } /* main */[/color]

More likely it looked like this:

for(;0;);
printf("Gud Morning");

Passing it through indent should make such obvious.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


Micah Cowan
Guest
 
Posts: n/a
#21: Mar 22 '06

re: explain this.....


"void * clvrmnky()" <clvrmnky.invalid@hotmail.com.invalid> writes:
[color=blue]
> Micah Cowan wrote:[color=green]
> > "Charles M. Reinke" <cmreinke@ece.gatech.edu> writes:
> >[color=darkred]
> >> "muttaa" <asgosa.eeegct@gmail.com> wrote in message
> >> news:1142886729.986974.281450@j33g2000cwa.googlegr oups.com...
> >> Any chance your actual code looked something like this:
> >>
> >> #include <stdio.h>
> >>
> >> int main(void) {
> >> int i;
> >>
> >> {
> >> for(;0;)
> >> i = 1;
> >> printf("Gud morning.\n");
> >> }
> >>
> >> return 0;
> >> } /* main */[/color]
> > Would it make a difference? The printf() would still never execute,
> > as
> > the 0 condition would result in the for's body never being executed,
> > and in any case, the value of i is never used anywhere.[/color]
>
> Read it again and note what those curly braces delimit. Put the curly
> braces around the code the way the compiler might see it.
>
> This discussion is not about correct code (since we have not seen any)
> but about the specifics of the control expression in a for()
> statement. That "i = 1" is there to illustrate a point and common
> mistake by novices.[/color]

Ah, quite. I have some trouble reading the original code, because my
newsreader actually substitutes for (;0;) with for (;0<smiley face>.
I think this is finally the last straw that makes me go look for where
to get that turned off.
Keith Thompson
Guest
 
Posts: n/a
#22: Mar 22 '06

re: explain this.....


Micah Cowan <micah@cowan.name> writes:
[...][color=blue]
> Ah, quite. I have some trouble reading the original code, because my
> newsreader actually substitutes for (;0;) with for (;0<smiley face>.
> I think this is finally the last straw that makes me go look for where
> to get that turned off.[/color]

<OT>gnus-treat-smiley?</OT>

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Vadivel
Guest
 
Posts: n/a
#23: Mar 22 '06

re: explain this.....


Muttaa will you repost the Snippet with full FOR loop construction.
Which will able to us to explain you. (if it is actually working)

Watch for your repost.

Vadivel P.

Vladimir S. Oka
Guest
 
Posts: n/a
#24: Mar 22 '06

re: explain this.....



Vadivel wrote:[color=blue]
> Muttaa will you repost the Snippet with full FOR loop construction.
> Which will able to us to explain you. (if it is actually working)
>
> Watch for your repost.[/color]

You're not only late to the party, but didn't bother to check the
dress-code either.

Reading these:

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

will help.

--
BR, Vladimir

santosh
Guest
 
Posts: n/a
#25: Mar 22 '06

re: explain this.....


Vadivel wrote:[color=blue]
> Muttaa will you repost the Snippet with full FOR loop construction.
> Which will able to us to explain you. (if it is actually working)
>
> Watch for your repost.
>
> Vadivel P.[/color]

See the thread titled 'help me atleast now'. In a nutshell, there
turned out to be a bug in the Turbo C++ compilers 1.01 and 3.0

As an aside please quote the post to which you're replying. For more
information read the following URLs.

<http://cfaj.freeshell.org/google/>
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
<http://en.wikipedia.org/wiki/USENET>
<http://en.wikipedia.org/wiki/Netiquette>
<http://www.safalra.com/special/googlegroupsreply/>

Closed Thread