problem with for loop

int main(void)
{
int x=0;
for(;x;)
{
printf("hello");
}
while(!kbhit());
return 0;
}
output

nothing


int main(void)
{
int x=0;
for(;0;)
{
printf("hello");
}
while(!kbhit());
return 0;
}

output
hello


Interesting, they should both output nothing as far as I can see.

1
2
int x=0;
for(;x;)


this never runs because 0 is false not true.
for loop only runs while condition is true (1 or more)
let me tell u that technically there is no condidtion in C becoz C language do not have boolean construct


"true" or "false"
I have no clue about C programing but I think you are teribly wrong.
I will kick my head off and sh*** down my nech if for loop in C differs from for loop in C++

cheers.
let me tell u that technically there is no condidtion in C


It is true that the C definition does not contain the word "condition". Nonetheless, ISO/IEC 9899, the official definition of C, indicates throughout its text that false means the same thing as zero, and true the same thing as nonzero.

ISO 14882 2003 (C++03 definition) makes it even clearer: 4.12 "A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true."

C++ does use the term condition to describe identical for loops. C may well technically not have conditions, but in non-technical reality, it does.


@codekiddy no I am not wrong "for" loop in C is totally diffrent from for loop in C++ but on visual studio IDE it takes true or false as condition but in C it does not take true or false as condition..................on any IDE

OK!,
big thatnks to Mr. Stroustrup for C++ so we don't have to learn C.
Last edited on
closed account (1vRz3TCk)
JAI SINGH,

There is no semantic difference between the for loops in C and C++. Both the pieces of code posted should produce no output under both C and C++. Weather C compare to Zero or C++ uses a specific boolean type makes no real difference. If you see a difference then please explain further.


P.S. C has had a boolean type since C99, it is in <stdbool.h> (Don't expect to find it in Microsoft's implementation of C, it is not compliant).
no I am not wrong "for" loop in C is totally diffrent from for loop in C++


Not just wrong, but so wrong that holding such a belief is damaging.
@CodeMonkey thanx sir


but on IDE TCPP,BORLAND C,and too many others IDE on which I have worked it gives an error

undefined symbol "true" or "false"

so tell me the IDE which support these type of functionalities
The IDE has nothing to do with it. It is the compiler that matters.

undefined symbol "true" or "false"


False is 0. True is any other number.
leave the topic condition in C

tell me solution of the problem why it is happening in ?
The solution is the best way is happening C has yes
closed account (1vRz3TCk)
I'm not entirely sure I understand what you are asking, however:

It is the compiler that is compliant/supports the features, not the IDE. For a list of compliant compilers see: http://en.wikipedia.org/wiki/C99

However if you just want to be able to use Boolean values in C , you could look at defining said values. See: http://c-faq.com/~scs/cgi-bin/faqcat.cgi?sec=bool

Hope it helps.
Topic archived. No new replies allowed.