Strange things happen with bool? [closed]

It’s tricky at first. But there is a simple reason why GCC complains.

(num == 21) ? a = TRUE : b = TRUE;

Here, the colon operator takes precedence over equal to operator. So effectivey, this statement is evaluated as

((num == 21) ? a = TRUE : b) = TRUE;

which in turn is the same as

TRUE = TRUE;

So the compiler rightly complains that lvalue required as left operand of assignment. However, if you use parenthesis to correct the affinity, it won’t complain.

(num == 21) ? (a = TRUE) : (b = TRUE); //No compiler error

Leave a Comment