> jump to navigation

edk's notes

order of evaluation

C tutorials and references frequently talk about the “order of evaluation” of expressions in C. On the face of it this is not unreasonable; it does make sense in algebra, after all. However, in C (and most languages that derive from it), the order in which things are evaluated is rarely specified at all. For example, in the following expression:

(x = y) + x

the expression in parenthesis is not evaluated first. In fact, the evaluation of (x = y) relative to the other evaluation of x is not ordered at all, and this expression causes undefined behaviour. Similarly, given an expression like:

a() + b() * c()

a(), b(), and c() must be evaluated in a particular order, but it is unspecified which order, and a strictly conforming program could not depend on the order being consistent with the grouping of the operands.

an incomplete guide to the actual order of evaluation

related common misconceptions