C Programming - Control Instructions - Discussion

Discussion Forum : Control Instructions - Find Output of Program (Q.No. 13)
13.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int x=1, y=1;
    for(; y; printf("%d %d\n", x, y))
    {
        y = x++ <= 5;
    }
    printf("\n");
    return 0;
}
2 1
3 1
4 1
5 1
6 1
7 0
2 1
3 1
4 1
5 1
6 1
2 1
3 1
4 1
5 1
2 2
3 3
4 4
5 5
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
46 comments Page 3 of 5.

Sahil said:   9 years ago
Before one increment of x, there is one printf statement due to which once x should be printed 1.

Renuka said:   1 decade ago
Can anyone please explain what is "for(; y; printf("%d %d\n", x, y))" ?

Andy said:   6 years ago
What is ; for after the bracket, before y?

for(; y; printf("%d %d\n", x, y))
(3)

Ashish said:   1 decade ago
If 6<=5 the condition will become false then how it will print last one i.e. 7 0.

Mohit singhal said:   1 decade ago
We can increase the value of x until the condition of y not become false.

Nandha said:   1 decade ago
If it's y = x++ <=5 false then boolean literal is 0, so y value is 0.

Pavani said:   1 decade ago
Incremented x value must be less than 5. How could it become 6 and x?

Mohit said:   1 year ago
@Manoj Wagh

Look at the question carefully;

this is x++ not ++x

Viki said:   1 decade ago
When the condition fails, the why will be zero, but the x will be 7.

Ravindar said:   1 decade ago
Thank for explanation to ravindara, kiran, and laxam.


Post your comments here:

Your comments will be displayed after verification.