Exforsys
+ Reply to Thread
Results 1 to 2 of 2

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 ...

  1. #1
    caradoc is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    122

    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.


  2. #2
    cyrus is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    128
    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.


Latest Article

Network Security Risk Assessment and Measurement

Read More...