Reputation: 2114
I made a typo while I was writing some C
code and noticed that CLion
did not see anything wrong with doing
while(line != NULL != 0)
In fact, it actually compiles and runs without error (though it does always return true)
I did some further testing, and found that it actually does return false sometimes
//true
if(1 != 0 == 1)
//false
if(1 != 0 == 0)
//true
if(1 != 0 > 2)
At first I thought it was essentially doing
if((1!=0) == true)
but that last check has me completely lost.
Upvotes: 2
Views: 47
Reputation: 4788
They are evaluated via correct order of operations, and remember false
evaluates to 0
and true
to 1
!=
and ==
have equal precedence, evaluate left to right
>
has greater precedence than !=
, evaluate it first
//true
if(1 != 0 == 1) => if ((1 != 0) == 1) => if (1 == 1) => true
//false
if(1 != 0 == 0) => if ((1 != 0) == 0) => if (1 == 0) => false
//true
if(1 != 0 > 2) => if (1 != (0 > 2)) => if (1 != 0) => true
http://en.cppreference.com/w/c/language/operator_precedence
Upvotes: 3