Explain Why does this C code give output False?

40 Views Asked by At
#include<stdio.h>
int main()
{
    int a=4,b=15,c=29;
    if(c>b>a)
    {
        printf("True");
    }
    else
    {
        printf("False");
    }
    return 0;
}

When I run the above code in my dev C++ compiler, it gives me the following output.

enter image description here

1

There are 1 best solutions below

0
Sourav Ghosh On

In your code

c>b>a

is same as

((c>b) > a)

The output of a relational operator, like > is either 0 or 1, and the type is int.

Either of those values, in this case, is less than the value of a, which is 4. So in your case (b=15,c=29), it becomes

(1 > a)

which is falsy.

If you want to chain the condition check, you should write

((c>b) && (b>a))