# if(a>b>c)

Discussion in 'C Programming' started by Adee, Feb 16, 2013.

Problem is :
let int a=5,b=16,c=7;

if(a<b<c)
{
printf("C is greater %d", c);
}

in above case still this statement executed. Any one explain the reason !

2. ### Lew PitcherGuest

On Saturday 16 February 2013 03:14, in comp.lang.c,
wrote:

> Problem is :
> let int a=5,b=16,c=7;
>
> if(a<b<c)
> {
> printf("C is greater %d", c);
> }
>
> in above case still this statement executed. Any one explain the reason !

Short answer: the "condition" part of an if() statement doesn't work the way
you think it works. Go back to your tutorial, and look up "Relational
Operators".

--
Lew Pitcher
"In Skills, We Trust"

Lew Pitcher, Feb 16, 2013

3. ### BartCGuest

news:...
> Problem is :
> let int a=5,b=16,c=7;
>
> if(a<b<c)
> {
> printf("C is greater %d", c);
> }
>
> in above case still this statement executed. Any one explain the reason !

Because (a<b<c) doesn't mean (a<b && b<c), if that's what you assumed.

It means ((a<b)<c), where a<b will be 0 or 1 (1 in this case).

--
Bartc

BartC, Feb 16, 2013
4. ### John BodeGuest

On Saturday, February 16, 2013 2:14:10 AM UTC-6, Adee wrote:
> Problem is :
>
> let int a=5,b=16,c=7;
>
> if(a<b<c)
> {
> printf("C is greater %d", c);
> }
>
> in above case still this statement executed. Any one explain the reason !

"a<b<c" doesn't work the way you are expecting it to.

From the C language standard (2011 version, n1570):

> 6.5.8 Relational Operators
> Syntax
> 1 relational-expression:
> shift-expression
> relational-expression < shift-expression
> relational-expression > shift-expression
> relational-expression <= shift-expression
> relational-expression >= shift-expression
> ...
> 6 Each of the operators < (less than), > (greater than), <= (less
> than or equal to), and >= (greater than or equal to) shall yield 1
> if the speciï¬ed relation is true and 0 if it is false.107) The result
> has type int.
> ___________
>
> 107) The expression a<b<c is not interpreted as in ordinary mathematics.
> As the syntax indicates, it means (a<b)<c; in other words, â€˜â€˜if a is less
> than b, compare 1 to c; otherwise, compare 0 to câ€™â€™.

In this case, the result of a<b is always going to be less than c, whether
a<b is true or not, so the result of the expression a<b<c will always be 1.

John Bode, Feb 21, 2013