A simple non-portable expression tha

Henry Spencer henry at utzoo.UUCP
Wed Apr 23 10:11:05 AEST 1986


> No, the compiler is not free to associate right-to-left in an
> expression
> 	long + int + int.
> Addition associates left-to-right, and the integral widening
> conventions apply, so the middle term must be converted to long
> and added to the leftmost term before the rightmost term is
> added.  The compiler is allowed to reorder this only if it
> guarantees the same answer as would be obtained by following
> the rules...

Sorry, Doug, not so.  Not in the draft standard, anyway, unless serious
changes have been made since my last copy (November).  Yes, there is a
statement early on to the effect that "the compiler can do things any
way it pleases if the results are the same".  However, somewhat later,
in C.3, we also find:

	An expression involving more than one occurrence of the same
	commutative and associative binary operator (*, +, &, ^, |)
	may be regrouped arbitrarily... provided the types of the
	operands or of the results are not changed by this regrouping.

Note that no mention is made of the types of intermediate values, unless
that ambiguous reference to "results" instead of "result" is meant to
imply it.  And there is no constraint that the value of the expression
be unchanged.
-- 
Support the International
League For The Derision		Henry Spencer @ U of Toronto Zoology
Of User-Friendliness!		{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list