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;
}
Discussion:
46 comments Page 2 of 5.
Atul tailwal said:
1 decade ago
If x=1, y=1
y=x++<=5 it means first times x=1 and y=1 (not y=2) because x++ is preincreament , means first increament then assign. so loop will repeat 6 times.
y=x++<=5 it means first times x=1 and y=1 (not y=2) because x++ is preincreament , means first increament then assign. so loop will repeat 6 times.
Rashmi said:
1 decade ago
Thank you atul.
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.
Kailash singh said:
1 decade ago
Thank you for explanation.
Ravindar said:
1 decade ago
Thank for explanation to ravindara, kiran, and laxam.
Panchanan rauta said:
1 decade ago
Here the execution as like below
step-1 y=x++<=5; here x is post increment '1' is compare<=5 true y=1
then x increment print x=2 y=1
step2 2<=5 x become 3 and y=1 print x=3 y=1
step3 3<=5 x become 4 and y=1 print x=4 y=1
step4 4<=5 x become 5 and y=1 print x=5 y=1
step5 5<=5 x become 6 and y=1 print x=6 y=1
step6 6<=5 x become 7 and y=0 print x=7 y=0
step7 it check y become false loop will terminate
Hence the result is
2 1
3 1
4 1
5 1
6 1
7 0
step-1 y=x++<=5; here x is post increment '1' is compare<=5 true y=1
then x increment print x=2 y=1
step2 2<=5 x become 3 and y=1 print x=3 y=1
step3 3<=5 x become 4 and y=1 print x=4 y=1
step4 4<=5 x become 5 and y=1 print x=5 y=1
step5 5<=5 x become 6 and y=1 print x=6 y=1
step6 6<=5 x become 7 and y=0 print x=7 y=0
step7 it check y become false loop will terminate
Hence the result is
2 1
3 1
4 1
5 1
6 1
7 0
Mohit singhal said:
1 decade ago
We can increase the value of x until the condition of y not become false.
Yash said:
1 decade ago
At first when x=1 and y=1 then the for loop condition is true then why there is no 1 1 in the output. I think in the result there is also 1 1 there.
I think the result will be :
1 1
2 1
3 1
4 1
5 1
6 1
7 0
Why the result is not like that? Please explain.
I think the result will be :
1 1
2 1
3 1
4 1
5 1
6 1
7 0
Why the result is not like that? Please explain.
Ranjana said:
1 decade ago
Because x got post incremented at each step.
Pavani said:
1 decade ago
Incremented x value must be less than 5. How could it become 6 and x?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers