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.
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.
Sai said:
1 decade ago
Thank you Kiran.
6 < = 5 is false, then why = 0, and x increments to 1 i.e., x = 7,
In this you are saying that when it false it will take y=0 but already the why is initialized with 1 how it will take ?
6 < = 5 is false, then why = 0, and x increments to 1 i.e., x = 7,
In this you are saying that when it false it will take y=0 but already the why is initialized with 1 how it will take ?
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?
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.
Gauravkumar said:
1 decade ago
@Yash.
Execution of loop takes this form.
Step 1 : Initialization.
Step 2 : Condition checking.
Step 3 : Execute body of loop.
Step 4 : Increment.
Execution of loop takes this form.
Step 1 : Initialization.
Step 2 : Condition checking.
Step 3 : Execute body of loop.
Step 4 : Increment.
Teja said:
1 decade ago
@pavani.
Incremented x value must be <=5 and we are using post increment. So before executing inc/dec in for loop body will be executed first.
Incremented x value must be <=5 and we are using post increment. So before executing inc/dec in for loop body will be executed first.
Sonam said:
1 decade ago
Why is the value of x not assigned in y?
coz, y=x++<=5.......????
what should have been the syntax to assign the value of x to y??
coz, y=x++<=5.......????
what should have been the syntax to assign the value of x to y??
Manju said:
1 decade ago
y=x++<=5; is the statement, when x evaluates to b 2 why x value is not assigned to y ? why y is still 1?
Ggf said:
1 decade ago
@yash.
You are wrong as per for loop rule first initialize then condition check then move over increment.
You are wrong as per for loop rule first initialize then condition check then move over increment.
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.
Bt I don't understood the last point when x=6 and y=0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers