Search code examples
cboolean-operations

Boolean output with comparison operator


Why the output of the following C code is 1 (True)?

#include<stdio.h>
main()
{
    int a, b = 1, c = 3, d = 2;
    a = b < c < d;
    printf("%d",a);
}

While the same expression gives "False" in python.


Solution

  • Check the order of evaluation from left to right.

    b<c is true so it returns 1.

    Then

    1<d Yes so you get 1

    So

    a=1

    Make sure since you are using relational operators the value returned will be true or false. i.e 0 or 1