0

The following code supposed to print true. But it is printing false. Does anyone know why is it so?

int main(void)
{
    int a=15,b=10, c=1;
    if(a>b>c)
    {
        printf("true");
    } else
    {
        printf("false");
    }
}
Anu
  • 1,123
  • 2
  • 13
  • 42
  • 1
    Those are relational operators, not logical operators, and nobody ever told you they would work that way. ā€“ Eric Postpischil Jul 14 '21 at 02:13
  • [Language support for chained comparison operators (x < y < z)](https://stackoverflow.com/q/4090845/995714), [Why do most mainstream languages not support ā€œx < y < zā€ syntax for 3-way Boolean comparisons?](https://softwareengineering.stackexchange.com/q/316969/98103) ā€“ phuclv Jul 14 '21 at 02:17

1 Answers1

3

In C, a>b>c means (a>b)>c. It does not mean (a>b)&&(b>c).

The value of a>b is either 0 or 1 (false or true, respectively). Since c is 1, neither of those possible values can be greater than c, so the comparison is always false.

rici
  • 234,347
  • 28
  • 237
  • 341