if (0)
evaluates the predicate 0
as a binary value. Binary values in C use integers, where zero means false and non-zero means true. Therefore, 0 will always evaluate to false.
For binary operators, being right- or left-associative determines the order that otherwise equally important operators will be processed. Consider the subtraction operator:
37 - 10 - 4
Both -
are equal precedence so which should we evaluate first? Well, -
is left-associative so we do:
(37 - 10) - 4 ==> (27) - 4 ==> 23
If the -
operator were right-associative, we would do:
37 - (10 - 4) ==> 37 - 6 ==> 31
Equality (=
) is right-associative because we might chain equalities together. So, if we see
// a and b are initialized to 0
a = b = 45
Both =
are equal precedence so we evaluate right to left and do:
a = (b = 45) // b receives 45
a = 45 // a receives 45
If we were to go left-right, we'd get an unexpected result:
(a = b) = 45 // a receives 0
b = 45 // b receives 45
For unary operators, however, ordering can only matter when multiple unary operators affect the same value. For example, let's do:
char x = 0xFF
bool y = !~x
These unary operators are right-associative, so we do:
!(~0xFF) ==> !(0x0) ==> true
In the example you showed, the negation operators affecting A
and E
didn't have "equal precedence" because they didn't have the same operand. So, associativity doesn't apply.