-1
int j=42;
if( (5<j<=1) ) {
    printf("yes");
} else {
    printf("no");
}

Output:

yes

Why does it output yes?
Isn't the condition only half true?

Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
  • 2
    You *can* do that, it just doesn't do what the OP thinks (of course, you probably *shouldn't* do that). – Ed S. Jan 08 '14 at 07:41
  • I'm curious as to why you think the output is wrong. What logic did you think the program would follow so that it would print "no"? Your explanation, "because 5" is a bit on the short side. – Mr Lister Jan 08 '14 at 07:50
  • This is not what I asked The person who edit my question has thought I wrote it wrong But it is not wrong He hasn't run the code before edit – Menuka Ishan Jan 08 '14 at 19:22
  • Then what _did_ you mean? The output of the program doesn't depend on the other, unused variables, so removing those only made the problem clearer. And your question, "But I cannot understand How that could happen?" was kept intact, even with the capitalisation error. That leaves the cryptic "because 5"... – Mr Lister Jan 09 '14 at 08:00
  • if condition have this (5 – Menuka Ishan Jan 10 '14 at 19:26
  • [Language support for chained comparison operators (x < y < z)](https://stackoverflow.com/q/4090845/995714), [Two '==' equality operators in same 'if' condition are not working as intended](https://stackoverflow.com/q/2155280/995714) – phuclv Jul 29 '17 at 06:20

4 Answers4

8

C does not understand math-like syntax, so

if(1<j<=5)

is not interpreted as you expect and want; it should be

if (1 < j && j <= 5)

or similar.

As explained in other answers, the expression is evaluated as

 ((1 < j) <= 5)

 =>  ("true" <= 5)

 =>  "true"

where "true" (boolean value) is implicitly converted to 1, as explaneid e.g. here, with references to standards too, and this explain why "true" has to be "less than" 5 (though in C might not be totally correct to speak about "implicit conversion from bool to int")

Community
  • 1
  • 1
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
6

As per operator precedence and LR associativity,

1<j evaluates to 1

1<=5 evaluates to 1

if(1)
{ 
  printf("yes")
Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
5

Your question is a bit broken, but I believe that the following will clarify what is going on for you:

In C, 1 < j <= 5 means the same thing as (1 < j) <= 5. And the value of 1 < j is 0 or 1 depending on whether is less than or equal to 1 or strictly greater than 1. So here is what happens for a few values of j in your code:

If j == 0, this expression is (1 < 0) <= 5, which reduces to 0 <= 5 (because 1 < 0 is false). This is a true expression. Your program outputs "yes".

If j == 3, this expression is (1 < 3) <= 5, which reduces to 1 <= 5 (because 1 < 3 is true). This is a true expression. Your program outputs "yes".

If j == 6, this expression is (1 < 6) <= 5, which reduces to 1 <= 5 (because 1 < 6 is true). This is a true expression. Your program outputs "yes".

In all cases, your program outputs "yes" because 1 < j is either 0 or 1, and either way it is less than 5.

What you should have used is 1 < j && j <= 5.

Blaise
  • 61
  • 3
2

what you want to write is if ( 1 < j && j <= 5 )

what is happening in your case is: if ( 1 < j <=5 )
1 < j is evaluated first, and it is true so it is evaluated to 1 and your condition becomes
if (1 <=5), which is also true so printf("yes"); gets excuted

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53