Next: , Previous: , Up: Order of Execution   [Contents][Index]


10.2 Associativity and Ordering

An associative binary operator, such as +, when used repeatedly can combine any number of operands. The operands’ values may be computed in any order.

If the values are integers and overflow can be ignored, they may be combined in any order. Thus, given four functions that return unsigned int, calling them and adding their results as here

(foo () + bar ()) + (baz () + quux ())

may add up the results in any order.

By contrast, arithmetic on signed integers, with overflow significant, is not really associative (see Integer Overflow). Thus, the additions must be done in the order specified, obeying parentheses and left-association. That means computing (foo () + bar ()) and (baz () + quux ()) first (in either order), then adding the two.

The same applies to arithmetic on floating-point values, since that too is not really associative. However, the GCC option -funsafe-math-optimizations allows the compiler to change the order of calculation when an associative operation (associative in exact mathematics) combines several operands. The option takes effect when compiling a module (see Compilation). Changing the order of association can enable the program to pipeline the floating point operations.

In all these cases, the four function calls can be done in any order. There is no right or wrong about that.