I have this part of an if statement and I\'m getting a weird output.
int x = 10;
if(1 < x < 5){
printf(\"F\\n\");
}
Why does it prin
In C, you can't chain comparisons like that. The expression 1 < x < 5
is evaluated as (1 < x) < 5
: so for x = 10
, the expression is (1 < 10) < 5
. (1 < 10)
is true, which C represents as the value 1
, so the expression reduces to 1 < 5
. This is always true, and your printf() if executed.
As level-999999 says, in C you need to explicitly combine single comparisons with &&
and ||
.