Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   if(a>b>c) (http://www.velocityreviews.com/forums/t957683-if-a-b-c.html)

 Adee 02-16-2013 08:14 AM

if(a>b>c)

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 02-16-2013 08:59 AM

Re: if(a>b>c)

On Saturday 16 February 2013 03:14, in comp.lang.c, asad.w.malik@gmail.com
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 02-16-2013 03:06 PM

Re: if(a>b>c)

> 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 02-21-2013 07:51 PM

Re: if(a>b>c)

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.

 All times are GMT. The time now is 09:08 AM.