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.

Bhargav said:   9 years ago
Thank you all my friends.

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

Mohit said:   1 year ago
@Manoj Wagh

Look at the question carefully;

this is x++ not ++x

Laxman said:   1 decade ago
Initialisation alredy done by y=1.... enters the loop y=x++<=5.increments x to 2 but treats x as 1 in this expression and stores x=2 in x memory location of x ..y=1<=5 true so return 1 and y value is i now repets the loop x and y values are printed as 2 1 again enteres the loop increments x to 3 but value of x ois treated as 2in this expression..y=2<=5 true..y=1 x=3 this repets till y=1 and x=5 are printed. again it enters the body of loop as condition y=1 satisfied...then y=x++<5 here the x is incremented to 6 but as its post incremented , for the expression y=x++<=5 it is treated as 5 and conditon(5<=5) true so y =1 but the value of x in memory changes to 6 .now again repeatin the loop.....(;y;printf("%d %d",x,y))..y=1 i.e condition true so value of x,y=6,1 is printed, so again enterin body of the loop..now x value is 6 and in y=x++<=5 x will be incremented to 7 in its memory location but treated as 6 in this expression ...as y=6<=5 false returns 0 so y=0... again repeatin the loop...first the value of x and y are printed as 0 7 ..then condition is cheked...y=0 loop terminates.

Kiran said:   1 decade ago
Here, x and y are inialized to 1,
In for loop, the condition statement is y = 1, it is true.
The next statements in for will executes.
y = x++ <= 5, here x++ will take the value 1 and (1 <= 5) is true x++ will becomes 2 1.e., x = 2 and y = 1 .
Then the incremented statement prints, x = 2 y = 1 it till upto x = 5 and y= 1

Here y = x++ <=5, evaluates to x = 6 (5 <= 5 it is true and x increment to 1) and
y = 1

Here 6 < = 5 is false, then y = 0, and x increments to 1 i.e., x = 7,
Then it prints x = 7 and y = 0.
Then Checks condition y = 0 which is fails, it comes out from the loop
The result is
2 1
3 1
4 1
5 1
6 1
7 0

Amar said:   1 decade ago
Thanks for the explanation!!

Nitin said:   1 decade ago
Thank you kiran for explain in this manner.

Bt I don't understood the last point when x=6 and y=0.

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

Bhaskar said:   1 decade ago
Thank you kiran.

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


Post your comments here:

Your comments will be displayed after verification.