what is the output and how is it?
what is the output and how is it?
You're the "programmer", you tell me.
For the file "output" which I've sent the o/p is 4.
but how is it 4 I dont know.
This would output the numbers 0 to 3.Code:#include<stdio.h> #include<conio.h> void main() { clrscr(); for(int i=0;i<=3;i++); printf("%d",i); getch(); }
You have some problems
- use of non-standard header, conio.h
- Use of void main
- Use of non-standard function, clrscr()
can u plz explain me what do u mean by non-standard header, conio.h?
if we put a semi colon ( at the end of any loop it would go into infinite loop but y it is giving the o/p as the values from 0 to3?
conio is not part of the c standard and isn't included in many compilers. So your code won't compile on many compilers. This is only a problem if you want to make your code portable or make it available to others or are trying to learn standard, portable c.
Not with a for loop. The for loop will run to its conclusion because it contains the condition which will cause it to cease looping.Originally Posted by singer_shant
Your statement:
is really:Code:for(int i=0;i<=3;i++);
which means loop as long as i is less than 3, doing nothing in the loop.Code:for (int i = 0; i <= 3; i++) ;
At the end of the loop, i has already been set to 4, it falls out of the loop because the condition is no longer true, and 4 is output to stdout.
hope this helps.....
thank you fgw_three. I did get u.
but if the question is like this :
for(;;
then what is the output
That is an infinite loop, it won't exit.Originally Posted by singer_shant
If you need to do something with ";)" in a post, be sure to check "Disable smilies in text" before you post.
Yep -- that's one way that you can infinite loop with a for.Originally Posted by cwr
I tend to avoid that one, though. While there ARE times that you want to loop infinitely and exit the program from within the loop, I usually use something along the lines of
instead ofCode:while (1) { /*do something, exit from within here */ }
Just habit I guess....Code:for (;;) { /* do something, exit from wthin here */ }