
- Forum
- Programming Talk
- C and C++
- Query on Evaluation
Query on Evaluation
This is a discussion on Query on Evaluation within the C and C++ forums, part of the Programming Talk category; My friend wrote a C program as below: #include <stdio.h> main() { int a=5,b=6,c=6; printf("%d",(c>=b>=a?300:400)); } I told the program ...
-
Query on Evaluation
My friend wrote a C program as below:
#include <stdio.h>
main()
{
int a=5,b=6,c=6;
printf("%d",(c>=b>=a?300:400));
}
I told the program would give a error thinking that comparison given in printf cannot be evaluated. But the output was 400. Can someone explain me how the program get solved.
-
The program will not give error. It evaluates all expressions taking the precedence and associativity of operators into consideration. In fact you can give even more operations than this. In your program first c is compared to b and then the result of this is compared with a. Since you have assigned c=6=b the first condition of comparison of c and b gets satisfied and so you have 1 placed in it which reduces the expression to
(1>=a?300:400)
Now 1 is less than the value of a which is assigned as 5 and so condition becomes false and so value results in 400.

Reply With Quote





