explain this..... | | |
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...... | | | | 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. | | | | 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"). | | | | 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. | | | | 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. | | | | 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. | | | | 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 | | | | 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 | | | | 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. | | | | 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");
} | | | | 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> | | | | 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] | | | | 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. | | | | 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 | | | | 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. | | | | 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. | | | | 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. | | | | 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. | | | | 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 | | | | 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/> | | | | 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. | | | | 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. | | | | 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. | | | | 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 | | | | 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/> |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,439 network members.
|