analyze output plz

S

Sweety

Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?
 
R

Russell Hanneken

Sweety said:
Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?

It doesn't, at least not on my system:

russell@axiom% cat > hello.c
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}
russell@axiom% tcc -o hello hello.c
russell@axiom% ./hello
russell@axiom%

Nor should it output anything, as far as I can tell. The for loop never
executes, because the loop condition is always false (0).
 
M

Mike Wahler

Sweety said:
Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?

It shouldn't. Which implementation are you trying this with?

(Note that if you happened to accidentally put a semicolon after the
'for' expression-list, you'd see the "hello" output. Is this the
*exact* code?)

-Mike
 
R

RCollins

Sweety said:
Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?

Wasn't this beat to death (in this newsgroup) a week or
so ago?
 
E

Ed Morton

Sweety said:
Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?

Wild stab in the dark - you had a previous version of this code with a
semi-colon after the for... line. You compiled that then changed the
code to the above and forgot to re-compile it before executing it.

Ed.
 
K

Keith Thompson

Plz
i need full explanation ?
#include<stdio.h>
int main()
{
for(;0;)
printf("hello") ;
return 0 ;
}

Why o/p results 'hello'?

You asked almost exactly this same question here on July 26, with the
subject "problem with for loop". I'm glad to see that you learned
*something* from the lengthy thread that resulted, since you're now
declaring "int main()" rather than "void main()", and you've added a
"return 0". ("int main(void)" would be even better, and you should
still have a newline at the end of the output, so apparently you
weren't completely paying attention.)

Also, it's not obvious to everyone that "o/p" means "output"; please
spell out the word.

You give no indication that you've posted this question before,
causing several people who didn't see the previous thread to waste
their time going over the same things.

The program as posted should produce no output on any conforming
implementation.

You received several possibly useful answers at the time. One was that
an extra semicolon:
"for (;0;);"
would explain what you're seeing, but you never actually confirmed
that the program you posted is *exactly* the same as the one you
compiled and executed. Another is that it's a known bug in Turbo C++
3.0. I suspect that's the actual explanation, but we have no way to
confirm it. If you can, try the same program with a different
compiler.

If you want our help, you need to pay more attention, participate in
the discussion, and answer followup questions. If the answers you got
the first time weren't good enough, asking the same question again
will only get you the same answers again.

Are you using Turbo C++ 3.0?

Is the code you posted the *exact* code that you compiled and executed?

Can you try the same code with a different compiler?

What led you to write this code in the first place? Writing a loop
like "for (;0;)" makes no sense in a real program; there are easier
ways to do nothing. Is there some underlying issue that you're trying
to understand, with the above code being an example that illustrates
it? If so, you'll get more meaningful answers if you'll tell us what
the actual point is. If you're just quizzing us, a lot of us are
probably willing to play along, but it's polite to let us know that
that's what you're doing rather than helping with a real-world
problem. Conversely, if it is a real-world problem, some background
would help us to understand it.

We're happy to help if we can, but you have to help us; so far, you
haven't.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top