explain this.....

M

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......
 
S

santosh

muttaa said:
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.
 
V

void * clvrmnky()

muttaa said:
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").
 
K

Keith Thompson

muttaa said:
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.
 
R

Robert Gamble

muttaa said:
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
 
V

Vladimir S. Oka

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.
 
D

Default User

muttaa said:
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
 
V

void * clvrmnky()

Keith said:
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.
 
M

Micah Cowan

muttaa said:
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");
}
 
P

Pedro Graca

muttaa said:
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
 
M

mianaome

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:
 
B

Ben Bacarisse

Ayon kay muttaa:
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.]
 
C

Charles M. Reinke

muttaa said:
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
 
C

CBFalconer

Charles M. Reinke said:
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/>
 
M

Micah Cowan

Charles M. Reinke said:
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.
 
V

void * clvrmnky()

Micah said:
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.
 
K

Keith Thompson

Micah Cowan said:
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.)
 
F

Flash Gordon

Micah said:
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.
 
S

Stephen Dedalus

Micah said:
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.
 

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

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top