473,765 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for(;0;) printf("hello") ;

for(;0;)
printf("hello") ;

even the condition s wrong i get a hello printed on the screen
y s this happening

Sep 10 '06
51 4144
Richard Heathfield <in*****@invali d.invalidwrites :
[...]
Gv th mn a bnn!
Give the man a bunny?

--
Keith Thompson (The_Other_Keit h) 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.
Sep 10 '06 #21
"Spidey" <am********@gma il.comwrites:
for(;0;)
printf("hello") ;

even the condition s wrong i get a hello printed on the screen
y s this happening
Your problem is that you don't yet understand how this newsgroup works.

Please don't use silly abbreviations. This is a technical discusssion
forum, not a chat room. Most of us can guess that "s" means "is" and
"y" means "why", but not everyone here has English as a first
language, and those of us who do have better things to do with our
time than figuring out what your abbreviations mean. (That's what
people have been trying to tell you with responses like
"Y h n p e c.".) If you want our help, take the time to write plain
English. (We won't complain about minor spelling and grammatical
errors.) Proper capitalization and punctuation are also very helpful.

What you *should* have written was something like this:

Even the condition is wrong. I get a "hello" printed on the
screen. Why is this happening?

(The first sentence is still unclear, but at least we can understand
the words.)

If you post a followup, provide context. You need to quote the article
you're replying to, or at least enough of it so that your followup
makes sense to someone who hasn't seen the previous article (but
delete anything that's not relevant to your reply). The Google Groups
interface does this for you. <http://cfaj.freeshell. org/google/has
some good links. See also <http://www.caliburn.nl/topposting.html >.
And see most of the articles in this newsgroup for examples of how
to do it right.

If you post a code sample, don't try to re-type it; copy and paste the
*exact* code that you compiled and executed. We can't tell which
errors were in your original code and which were introduced when you
transcribed it; don't ask us to guess. If at all possible, post a
small complete program that we can try, not just a code fragment.
Very often the actual error is not where you think it is. (If you
knew where the error was, you wouldn't have to ask us.)

I'm going to make a guess about what your problem is. You posted
this:

for(;0;)
printf("hello") ;

but your program really contains this:

for(;0;);
printf("hello") ;

The semicolon on the first line indicates a null statement, which is
the statement controlled by the for loop. The printf() call follows
the for loop; it's not part of it. So you do nothing zero times, then
call printf() unconditionally .

(Why would you write "for(;0;)" anyway? There are easier ways to do
nothing.)

Recommended reading:

http://clc-wiki.net/wiki/Introduction_to_comp.lang.c
http://www.c-faq.com/

--
Keith Thompson (The_Other_Keit h) 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.
Sep 10 '06 #22
"Spiros Bousbouras" <sp****@gmail.c omwrites:
Keith Thompson wrote:
>Richard Heathfield <in*****@invali d.invalidwrites :
[...]
Gv th mn a bnn!

Give the man a bunny?

Banana !
Give the banana a bunny?

--
Keith Thompson (The_Other_Keit h) 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.
Sep 10 '06 #23
Frederick Gotham <fg*******@SPAM .comwrites:
Michal Nazarewicz posted:
>You have put ';' after ')' and haven't noticed it?

I see nothing wrong with the code fragment:

for(;0;)
printf("hello") ;
Nighter do I but who can guarantee that OP compiled this code? If you
look at indention it may seem that he has compiled something
completely different which correctly produces /hello/.

Seems like Richard Heathfield came to the same conclusion.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >--<jid:mina86*jab ber.org>--ooO--(_)--Ooo--
Sep 10 '06 #24
Keith Thompson wrote:
"Spidey" <am********@gma il.comwrites:
for(;0;)
printf("hello") ;

even the condition s wrong i get a hello printed on the screen
y s this happening
[snip]
I'm going to make a guess about what your problem is. You posted
this:

for(;0;)
printf("hello") ;

but your program really contains this:

for(;0;);
printf("hello") ;
No. His program does contain the code he has provided here (without the

semicolon at the end of for statement). That is actually a compiler bug
of
Turbo C++ 3.0. Its big time he got an upgrade from that compiler.

To OP(Spidey):
Turbo C++ has released a new compiler recently and its for free, so,
get an
upgrade and your code will work fine. Its available from
http://turboexplorer.com

Sep 10 '06 #25
Spidey wrote:
am using turbo c3 n it wont put up any warning.
and the code produced the hello output on my screen
Turbo C is known to be buggy.

Here is a program that exposes bugs on many compilers:

#include <stdio.h>

int main(void)
{
int arr[5];
printf("%d\n", (int)sizeof arr);
printf("%d\n", (int)sizeof &arr);
return 0;
}

The size of &arr should be the size of a pointer, probably just 2, 4 or
8 bytes. Many compilers miss the & and instead give the same value as
the size of arr, a multiple of 5.

Turbo C 2.01: 10, 10 (wrong)
Borland C/C++ 5.5: 20, 20 (wrong)
MS cl 13.10.3077: 20, 20 (wrong)
LCC-Win32 3.8: 20, 4 (right)
GCC 3.4.4: 20, 4 (right)

Not a good showing for Borland or Microsoft. Even when invoked in
"standard-conforming" mode, they fail this basic C89 test.

--
Simon.
Sep 10 '06 #26
Spidey wrote:
am using turbo c3 n it wont put up any warning.
and the code produced the hello output on my screen
C:\docs\prog\c> type for0.c
#include <stdio.h>

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

return 0;
}

C:\docs\prog\c> tcc for0.c
Turbo C Version 2.01 Copyright (c) 1987, 1988 Borland International
for0.c:
Warning for0.c 6: Unreachable code in function main
Turbo Link Version 2.0 Copyright (c) 1987, 1988 Borland International

Available memory 392912

C:\docs\prog\c> for0
As you can see, it does put up a warning, and it does not produce hello
on my screen. It works perfectly. This code does not expose a bug in the
compiler (but see my other article which gives an example that does
expose a bug).

You must not be compiling the same code as me.

--
Simon.
Sep 10 '06 #27
Spidey wrote:
am using turbo c3 n it wont put up any warning.
and the code produced the hello output on my screen
Get a decent compiler, and until you do, *please* don't post questions.
Igmar

Sep 10 '06 #28
Simon Biber wrote:
for(;0;)
printf("hello") ;
Would it kill you to use curly braces?
Sep 10 '06 #29
jmcgill wrote:
Simon Biber wrote:
> for(;0;)
printf("hello") ;

Would it kill you to use curly braces?
To what end? Add clutter?

Constructs like..

for (;0;) {
printf("world") ;
}

...add no special readability for me (except the indent of course).
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 10 '06 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.