Math-like chaining of the comparison operator - as in, "if ( (5<j<=1) )" [duplicate]
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?
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")
As per operator precedence and LR associativity,
1<j
evaluates to 1
1<=5
evaluates to 1
if(1)
{
printf("yes")