C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 11)
11.
What will be the output of the program?
#include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x<=55, x=40, x>=10);
return 0;
}
Answer: Option
Explanation:
Step 1: int x=55; here variable x is declared as an integer type and initialized to '55'.
Step 2: printf("%d, %d, %d\n", x<=55, x=40, x>=10);
In printf the execution of expressions is from Right to Left.
here x>=10 returns TRUE hence it prints '1'.
x=40 here x is assigned to 40 Hence it prints '40'.
x<=55 returns TRUE. hence it prints '1'.
Step 3: Hence the output is "1, 40, 1".
Discussion:
16 comments Page 1 of 2.
Sri Krishna said:
1 decade ago
Yes, in printf the execution of expressions is from Right to Left. so first x>=10 is evaluated, which happens to be true so output of 3rd expression will be 1. now in the middle one, x is assigned the value of 40, and the final leftmost expression asks whether x<=55, which is true as the new value of x is 40.
This should be the way of evaluation of expressions in the answer.
Either way the answer turns out be 1,40,1.
This should be the way of evaluation of expressions in the answer.
Either way the answer turns out be 1,40,1.
Arvind said:
1 decade ago
But, there how if the condition is true means that will display 1 as output, can any one clear my confusion.
Ankitsitm.mishra00@gmail.com said:
1 decade ago
main ()
{
int I;
I = (1,2,3);
printf( " % d ", I );
I=1,2,3;
printf(" % d ", I );
}
Output of this question?
{
int I;
I = (1,2,3);
printf( " % d ", I );
I=1,2,3;
printf(" % d ", I );
}
Output of this question?
Daisy J said:
10 years ago
@ Ankitsitm.
Hi dear,
Output will be:
(i) 3.
(ii) 1.
Hi dear,
Output will be:
(i) 3.
(ii) 1.
Lov singh said:
10 years ago
#include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x<=55, x=40, x>160);
return 0;
}
It's output is :-
1, 40, 0.
So how can you say that in printf the execution of expressions is from Right to Left.
int main()
{
int x=55;
printf("%d, %d, %d\n", x<=55, x=40, x>160);
return 0;
}
It's output is :-
1, 40, 0.
So how can you say that in printf the execution of expressions is from Right to Left.
Lov singh said:
10 years ago
Ok I got it.
And I think think this ex. Will help others who are in doubt:
#include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x>=55, x=40, x>160);
return 0;
}
And its output is:
0, 40, 0.
And I think think this ex. Will help others who are in doubt:
#include<stdio.h>
int main()
{
int x=55;
printf("%d, %d, %d\n", x>=55, x=40, x>160);
return 0;
}
And its output is:
0, 40, 0.
Ankit Kumar said:
9 years ago
I didn't understand the output 1, 40, 1 since x is definitely assigned with 40 but it should rather return a status value such as 1 or 0 and not 40. Please explain this.
Raji said:
9 years ago
Can anyone explain my doubt in this problem how X is greater than equal to 10 will be satisfied?
Hritik said:
9 years ago
Why this expression execute from right to left?
Ranjit gupta said:
8 years ago
How it returns and print 1?
(here x>=10 returns TRUE hence it prints '1'.)
But if true returns,it prints 0
And id false returns,it prints 1,
Why mentioned like that returns TRUE hence it prints 1.
Didn't get this logic. Explain someone.
(here x>=10 returns TRUE hence it prints '1'.)
But if true returns,it prints 0
And id false returns,it prints 1,
Why mentioned like that returns TRUE hence it prints 1.
Didn't get this logic. Explain someone.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers