if(a>b>c)

A

Adee

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

Lew Pitcher

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".
 
B

BartC

Adee said:
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).
 
J

John Bode

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top