Theory of Pure C, chapter 937

utzoo!decvax!harpo!esquire!cmcl2!lanl-a!jhf utzoo!decvax!harpo!esquire!cmcl2!lanl-a!jhf
Thu Jan 6 16:26:24 AEST 1983


Gary Mann (we53!glm) says he would expect that in the function call

	foo ( x++ * x)

the value "x times x incremented" be passed to foo.  In fact, he has
no good reason for expecting this, aside from demanding that his
compiler execute expressions strictly left-to-right.  As has been
pointed out previously in this discussion, the order of evaluation of
expressions, and the timing of side effects of an expression are \not/
defined.  (See section 7 of the C reference manual.)  Thus, in the above
example, if x is initially 2, the value passed to foo can be either
4 or 6.  All one can depend on is that when the next statement is executed,
x will have been incremented.

Another way of expressing the philosophy of side-effects in C is this --
It's all right for expressions to have side effects.  (In fact, since
C is an expression language, that's the only way we have any effects at all!)
But the effects of executing an expression must be independent of each
other and of the evaluation of the expression.

I would not want to have compiler writers forced to give some consistent
semantics to expressions such as the above; rather, they should be given
free rein in generating code for expressions, to produce compact and
efficient code.



More information about the Comp.lang.c mailing list