Re: 'C' Operators precedence

David Todd (dtodd@bbn.com)
Wed, 20 May 1998 11:54:42 -0400


>From "C - A Reference Manual", Fourth Edition by Harbison and Steele:

In general, the compiler can rearrange the order in which
an expression is evaluated. The rearrangement may consist
of evaluating only the arguments of a function call, or
the two operands of a binary operator, in some order other
than the obvious left-to-right order. The binary operators
+, *, &, ^, and | are assumed to be completely associative
and commutative, and a compiler is permitted to exploit this
assumption.

The upshot:

int a = 0;
int f() {a=2; return 1}
int g() {a=1; return 1}

main ()
{
printf (a + f()); /* could print 1 or 3 */
printf (g() + f()); /* will print 1, a could = 1 or 2 */
printf (g() + a + f()); /* could print 2, 3, or 4 */
}

The compiler is merely obliged to not interfere with type promotion.

So, the important thing is don't write functions with side effects and DON'T
USE GLOBALS.

-- 
Hacksaw = David Charles Todd
GTEI-BBNT = Hacksaw's Employer
Hacksaw's Opinions != GTEI-BBNT's Opinions
Linux understands you.

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu