0

i executed this following code in C;

#include<stdio.h>
#include<conio.h>
void main()
{
        int a=15,b=10,c=5;
        if(a>b>c)
           printf("True");
        else
           printf("False");
        getch();
}

output: False Please explain me why?

Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021
Rush W.
  • 1,301
  • 2
  • 10
  • 16

1 Answers1

3

There is no ternary (or 'chained') > operator in C or C++. Thus your expression evaluates to ((a>b)>c) as evaluation is done left to right.

In C, true expressions evaluate to 1, and false ones to 0. In C++ my recollection is that they evaluate to boolean true or false, but then these type convert to 1 or 0 anyway, so the situation is much the same.

Using that principle, a>b will evaluate to 1 if a>b and to 0 otherwise. Therefore if a>b, the entire expression evaluates to 1>c, else to 0>c. As c is more than one, neither 1>c nor 0>c are true, and the output is always 0, or false, and the program will print False.

To achieve what I strongly suspect you really want, use ((a>b) && (b>c)).

abligh
  • 23,834
  • 3
  • 44
  • 83