problem with for loop

S

Sweety

hello,
can any one tell why


void main()
{
for(;0;)
printf("hello") ;
}


o/p results 'hello'?
 
K

Keith Thompson

hello,
can any one tell why


void main()
{
for(;0;)
printf("hello") ;
}


o/p results 'hello'?

It doesn't. When I compile and run this program, I get some warnings
at compile time, and no output at all at run time. Are you sure that
the program you posted is exactly the same as the one you compiled?
An extra semicolon on the line with the "for" would explain the
behavior you're seeing. If you're going to post code samples, it's
very important to cut-and-paste the *exact* code that you fed to the
compiler.

The main function returns int, not void. It's better style (though
not strictly required) to declare explicitly that main has no
parameters rather than an indefinite parameter list.

A call to the printf function is invalid unless there's a valid
declaration in scope; the best way to get this declaration is to
include the <stdio.h> header.

Since you don't print a newline character, there's no guarantee that
any output will actually appear even if the printf call is executed.
You could follow it with a call to fflush(stdout) or, better yet,
print a newline character.

You fall off the end of main() without returning a value. This is
allowed in C99, but causes an undefined status to be returned to the
environment in C90. Even in C99, it's good style to have an explicit
return anyway.

You don't have braces around the single statement controlled by the
for loop. This is perfectly legal and very common, but you might
consider developing the habit of adding the braces anyway. It can
avoid problems when you later want to add another statement to the
body of the for loop.

Here's a revised version of your program:

#include <stdio.h>
int main(void)
{
for (;0;) {
printf("hello\n");
}
return 0;
}

It still produces no output, because the body of the for loop is never
executed.

Your original program includes at least two instances of undefined
behavior, but it's unlikely that either is actually going to cause
visible problems. Try compiling and running the revised version of
the program. If it actually produces any output, let us know.
 
E

E. Robert Tisdale

Something said:
hello,
can any one tell why


void main()
{
for(;0;)
printf("hello") ;
}


o/p results 'hello'?

This is an obvious troll. Please ignore it.
 
E

Emmanuel Delahaye

Sweety wrote on 26/07/04 :
hello,
can any one tell why


void main()
{
for(;0;)
printf("hello") ;
}

o/p results 'hello'?

I don't exactly know what 'o/p' means (could be 'output' BTW), but this
code is invoking an undefined behaviour because there is no prototype
for printf().

(FAQ) Additionally, there is no guarantee that the "hello" string will
be output because a '\n'is missing after the string, (or a
fllush(stdout) after the printf())

Not to mention that main() returns int hence an explicit and valid
value must be returned.

Considering your code fixed and commented ...

#include <stdio.h>

int main (void)
{
for(/* do nothing */;0 ;/* do nothing else */)
{
printf ("hello\n");
}
return 0;
}

.... it is semantically equivallent to

#include <stdio.h>

int main (void)
{
/* do nothing */
do
{
printf ("hello\n");

/* do nothing else */
}
while(0);

return 0;
}

.... hence the printf() is executed once.
 
E

Emmanuel Delahaye

Emmanuel Delahaye wrote on 27/07/04 :
... it is semantically equivallent to

#include <stdio.h>

int main (void)
{
/* do nothing */
do
{
printf ("hello\n");

/* do nothing else */
}
while(0);

return 0;
}

Sorry, I've got it wrong. It is equivallent to

#include <stdio.h>

int main (void)
{
/* do nothing */
while (0)
{
printf ("hello\n");
/* do nothing else */
}
return 0;
}

hence the printf() is not executed at all.
 
K

Keith Thompson

Emmanuel Delahaye said:
Considering your code fixed and commented ...

#include <stdio.h>

int main (void)
{
for(/* do nothing */;0 ;/* do nothing else */)
{
printf ("hello\n");
}
return 0;
}

... it is semantically equivallent to

#include <stdio.h>

int main (void)
{
/* do nothing */
do
{
printf ("hello\n");

/* do nothing else */
}
while(0);

return 0;
}

... hence the printf() is executed once.

No, it's not executed at all, because the condition (0) is always
false.

My best guess is that the original poster had an extra semicolon on
the for loop, causing the printf call to be outside the loop, and
therefore executed once.
 
S

Sweety

hello !!!
i m not novice on the regard while i have to face erxperts on groups.
This code really have strange behaviour plz go thru why it happens
in TC++ 3.0 compiler.
bye,
 
M

Martin Ambuhl

Sweety said:
hello !!!
i m not novice on the regard while i have to face erxperts on groups.

If you're not a novice, then you are a troll. It is more likely that
you are a novice and too stupid to know it.
 
K

Keith Thompson

hello !!!
i m not novice on the regard while i have to face erxperts on groups.
This code really have strange behaviour plz go thru why it happens
in TC++ 3.0 compiler.
bye,

Please don't top-post. Your response should follow any quoted text
from previous articles, and you should trim anything that's not
relevant.

In my previous response, I speculated that you might have an extra
semicolon on your for loop, and that the code you posted might not be
the actual code that you compiled. You haven't actually told us
whether that's the case or not. Telling us that you're "not novice"
doesn't help us to figure out why your program is misbehaving. If we
ask you to clarify something, you need to clarify it. We can't assume
that you understand something unless you actually demonstrate it.

Assuming that the code you posted is the actual code you compiled, and
that your program's output really is as you described it, you've
probably run into a bug in TC++ 3.0. See

<http://groups.google.com/groups?as_umsgid=<[email protected]>>

or, equivalently,

<http://tinyurl.com/592yv>

The referenced article was a response in this thread, posted just 5
days ago. Unless you've having problems with your Usenet feed, you
should already have seen it.

I've never used TC++ 3.0, and I don't know anything about this bug.

I previously gave you a revised version of your program:

#include <stdio.h>
int main(void)
{
for (;0;) {
printf("hello\n");
}
return 0;
}

Try compiling and running the revised program and let us know what
output you get. It should produce no output. If it prints "hello",
you're probably running into a compiler bug; in that case, perhaps
someone else can help you figure out what to do next.
 

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
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top