By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,334 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,334 IT Pros & Developers. It's quick & easy.

explain this.....

P: n/a
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......

Mar 20 '06 #1
Share this Question
Share on Google+
24 Replies


P: n/a
muttaa wrote:
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......


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.

Mar 20 '06 #2

P: n/a
muttaa wrote:
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).

Not for me. That expression _should_ evaluate to false (well, actually,
"non-zero").
Mar 20 '06 #3

P: n/a
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
muttaa wrote:
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).

Not for me. That expression _should_ evaluate to false (well,
actually, "non-zero").


Non-zero is true; zero is false.

--
Keith Thompson (The_Other_Keith) ks***@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.
Mar 20 '06 #4

P: n/a
"muttaa" <as***********@gmail.com> writes:
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......


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) ks***@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.
Mar 20 '06 #5

P: n/a
muttaa opined:
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......


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

--
BR, Vladimir

MICRO:
Thinker toys.

Mar 20 '06 #6

P: n/a
muttaa wrote:
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).


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

Robert Gamble

Mar 20 '06 #7

P: n/a
muttaa wrote:
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......

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
Mar 20 '06 #8

P: n/a
Keith Thompson wrote:
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
muttaa wrote:
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).

Not for me. That expression _should_ evaluate to false (well,
actually, "non-zero").


Non-zero is true; zero is false.

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.
Mar 20 '06 #9

P: n/a
"muttaa" <as***********@gmail.com> writes:
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......


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");
}
Mar 20 '06 #10

P: n/a
muttaa wrote:
i've a code snippet below:
for(;0;)

printf("Gud Morning");
When I try this in my compiler it outputs:
error: parse error before "for"

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


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

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 20 '06 #11

P: n/a
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:
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......


Mar 21 '06 #12

P: n/a
On Mon, 20 Mar 2006 20:27:06 -0800, mianaome wrote:
Ayon kay muttaa:
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).

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


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.
Mar 21 '06 #13

P: n/a
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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......


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
Mar 21 '06 #14

P: n/a
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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......


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 */


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.
Mar 21 '06 #15

P: n/a
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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......

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 */


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.


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.
Mar 21 '06 #16

P: n/a
Micah Cowan <mi***@cowan.name> writes:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
> 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......
>


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 */


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.


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) ks***@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.
Mar 21 '06 #17

P: n/a

Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
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......


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 */


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.


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

Mar 21 '06 #18

P: n/a
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:


<snip>
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 */


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.


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
Mar 21 '06 #19

P: n/a
"Charles M. Reinke" wrote:
"muttaa" <as***********@gmail.com> wrote in message
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......


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 */


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/>
Mar 21 '06 #20

P: n/a
"void * clvrmnky()" <cl**************@hotmail.com.invalid> writes:
Micah Cowan wrote:
"Charles M. Reinke" <cm******@ece.gatech.edu> writes:
"muttaa" <as***********@gmail.com> wrote in message
news:11**********************@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 */

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.


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.


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.
Mar 22 '06 #21

P: n/a
Micah Cowan <mi***@cowan.name> writes:
[...]
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.


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

--
Keith Thompson (The_Other_Keith) ks***@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.
Mar 22 '06 #22

P: n/a
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.

Mar 22 '06 #23

P: n/a

Vadivel wrote:
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.


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

Mar 22 '06 #24

P: n/a
Vadivel wrote:
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.


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/>

Mar 22 '06 #25

This discussion thread is closed

Replies have been disabled for this discussion.