So, I was writing some code and I was getting an unexpected output in one part of my program which disrupted the entire system.
I managed to extract and simplify th
&&
has higher precedence than ||
.
(See https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B )
so
++i || ++j && k
is
++i || (++j && k)
and ||
shortcircuits if the first operator is truthy, as per 6.5.14p4 .
If you're on gcc
or clang
and compile your code with -Wall
, the compiler will nudge you to put those parentheses there. It's probably a good idea to heed that advice, as some people get confused by the precedences (I hear).