Velocity Reviews > if(a>b>c)

if(a>b>c)

Guest
Posts: n/a

 02-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 !

Lew Pitcher
Guest
Posts: n/a

 02-16-2013
On Saturday 16 February 2013 03:14, in comp.lang.c, http://www.velocityreviews.com/forums/(E-Mail Removed)
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"

BartC
Guest
Posts: n/a

 02-16-2013

"Adee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

John Bode
Guest
Posts: n/a

 02-21-2013
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.