In the C language, there are two different issues you need to be aware of: operator precedence and order of evaluation.
Operator precedence determines which operator that gets its operands evaluated first, and also which operands that belong to which operator. For example in the expression a + b * c
, the operator * has higher operator precedence than +. Therefore the expression will be evaluated as
a + (b * c)
All operators in the C language have deterministic precedence and they are the same on any compiler.
Order of evaluation determines which operand that gets evaluated first. Note that a sub-expression is also an operand. Order of evaluation is applied after the operator precedence has been determined. If the above a+b*c example has left-to-right order of evaluation, then the operands themselves get evaluated in the order a, b, c. If the operands were for example function calls, then the functions a(), b() and c() would have been executed in that order. All operands in this example need to be evaluated since they are all used. If the compiler can determine that some operands need not be evaluated, it can optimize them away, regardless of whether those operands contain side-effects (such as function calls) or not.
The problem is that order of evaluation of operands is most often unspecified behaviour, meaning that the compiler is free to evaluate either left-to-right or right-to-left, and we cannot know or assume anything about it. This is true for most operands in C, save for a few exceptions where the order of evaluation is always deterministic. Those are the operands of the operators || && ?: ,
, where the order of evaluation is guaranteed to be left-to-right. (When using formal C language semantics, one says that there is a sequence point between the evaluation of the left and the right operator.)
So for the specific example m = ++i || ++j && ++k
.
- The unary prefix ++ operators have the highest precedence, binding the operators i, j and k to them. This syntax is pretty intuitive.
- The binary && operator has 2nd highest precedence, binding the operators
++j
and ++k
to it. So the expression is equivalent to m = ++i || (++j && ++k)
.
- The binary || operator has 3rd highest precedence, binding the operators
i++
and (j++ && ++k)=
to it.
- The assignment operator = has the lowest precedence, binding the operators
m
and ++i || (++j && ++k)
to it.
Further, we can see that both the || and the && operators are among those operators where the order of evaluation is guaranteed to be left to right. In other words, if the left operand of the || operator is evaluated as true, the compiler does not need to evaluate the right operand. In the specific example, ++i
is always positive, so the compiler can perform quite an optimization, effectively remaking the expression to m = ++i;
If the value of i
wasn't known at compile time, the compiler would have been forced to evaluate the whole expression. Then the expression would have been evaluated in this order:
- && has higher precedence than ||, so start evaluating the && operator.
- && operator is guaranteed to have order of evaluation of operands left-to-right, so perform ++j first.
- If the result of ++j is true (larger than zero), and only then, then evaluate the right operator: perform ++k.
- Store the result of ++j && ++k in a temporary variable. I'll call it
j_and_k
here. If both ++j and ++k were positive, then j_and_k
will contain value 1 (true), otherwise 0 (false).
- || operator is guaranteed to have order of evaluation of operands left-to-right, so perform ++i first.
- If ++i is false (zero), and only then, evaluate the right operator "
j_and_k
". If one or both of them are positive, the result of the || operator is 1 (true), otherwise it is 0 (false).
- m gets assigned the value 1 or 0 depending on the result.