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=...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
![]() |
| Thread Tools | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to find second max sal of employee with the co | chandraismoon | Oracle Tutorials | 7 | 05-16-2008 04:08 AM |
| trial version winrunner | garima | Testing Tutorials | 4 | 07-10-2007 02:11 AM |
| Performance of Query | Ralph | MySQL | 2 | 05-08-2007 05:44 PM |
| SQL QUERY IN REPORTS 6i:Pls help | nitinkoshymech | Oracle Database | 1 | 11-27-2006 10:34 AM |
| how to use the same form for query mode and input mode | bhaskara avunoori | Oracle Apps | 1 | 06-23-2006 07:07 AM |