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 4 of 5.
Abhishek Mahajan said:
1 decade ago
Look when inside the loop the expression (y = x++ <= 5) gets evaluated, then it is the value of x not the incremented value gets compared. Post increment operator increases the value after the variable's use.
SO the things will proceed like this:
1] y = x++ <= 5.
x=1 will get compared and after comparison.
y=1, x=2.
then print statement is executed for x=2 and y=1 (explained above).
2] x=2 will get compared and after comparison.
y=1, x=3.
then print statement is executed for x=3 and y=1 (explained above).
3] x=3 will get compared and after comparison.
y=1, x=4.
then print statement is executed for x=4 and y=1 (explained above).
4] x=4 will get compared and after comparison.
y=1, x=5.
then print statement is executed for x=5 and y=1 (explained above).
5] x=5 will get compared and after comparison.
y=1, x=6.
then print statement is executed for x=6 and y=1 (explained above).
6] x=6 will get compared and after comparison.
y=0, x=7.
then print statement is executed for x=7 and y=0.
Now after 6th step y=0, therefore the loop execution condition failed and loop will be exited.
SO the things will proceed like this:
1] y = x++ <= 5.
x=1 will get compared and after comparison.
y=1, x=2.
then print statement is executed for x=2 and y=1 (explained above).
2] x=2 will get compared and after comparison.
y=1, x=3.
then print statement is executed for x=3 and y=1 (explained above).
3] x=3 will get compared and after comparison.
y=1, x=4.
then print statement is executed for x=4 and y=1 (explained above).
4] x=4 will get compared and after comparison.
y=1, x=5.
then print statement is executed for x=5 and y=1 (explained above).
5] x=5 will get compared and after comparison.
y=1, x=6.
then print statement is executed for x=6 and y=1 (explained above).
6] x=6 will get compared and after comparison.
y=0, x=7.
then print statement is executed for x=7 and y=0.
Now after 6th step y=0, therefore the loop execution condition failed and loop will be exited.
(1)
Ashish said:
1 decade ago
If 6<=5 the condition will become false then how it will print last one i.e. 7 0.
Krish said:
1 decade ago
I will explain this in easy manner.
We have [y=(x++<=5)] if u observe carefully it consists of
2 valuable expression with in this.
Simple funda is: T T T(1)
F F F(0)
If x<=5 is (true) then expression (x++<=5) is always (true) , that means it have the value 1 , and y will be true
always(1).
If x>=5 is (false) then expression (x++>=5) is always (false) ,that means it have the value 0 , and y will be false always(0).
That is why last step will be [7 0].
The above program can be write like this in while loop:
while(y)
{
y=x++<=5;
pf("%d %d"x,y);
}
We have [y=(x++<=5)] if u observe carefully it consists of
2 valuable expression with in this.
Simple funda is: T T T(1)
F F F(0)
If x<=5 is (true) then expression (x++<=5) is always (true) , that means it have the value 1 , and y will be true
always(1).
If x>=5 is (false) then expression (x++>=5) is always (false) ,that means it have the value 0 , and y will be false always(0).
That is why last step will be [7 0].
The above program can be write like this in while loop:
while(y)
{
y=x++<=5;
pf("%d %d"x,y);
}
Ibu said:
1 decade ago
But why the "y" value did not change? In y=x++<=5; after the first incrementation y=2++<=5. Why shouldn't the incremented value of x=2 is assigned in "y"? can anyone explain?
Jitender Chhirang said:
1 decade ago
Because here y takes the value true or false if true than it get prints 1 otherwise 0 you can change the value of y in the program and check that value of y doesn't matter it will only work on true or false of x++<=5 condition it will print 1 until this condition becomes true and value of x is incremented in every step after the use of x and the incremented value is stored at the position of previous value of x and that's why it take the value from that address and prints the value but it doesn't take value of y from that position because it only takes the answer of true false condition.
Krithik said:
10 years ago
Thanks everyone for giving the time to explain this.
Nandini said:
9 years ago
Thanks @Kiran.
(1)
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.
Vignesh said:
8 years ago
Thanks @Kiran.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers