[net.lang.c] Unary plus

meissner@rtp47.UUCP (Michael Meissner) (08/06/85)

In article <177@hoqam.UUCP> twb@hoqam.UUCP (BEATTIE) writes:
>
>I was under the impression that the parentheses already guaranteed
>grouping. Do any/some/most/all compilers add a and b and then c?
>When is (a+(b+c)) not equal to ((b+c)+a)?
>

To quote K&R, page 185:

	Expressions involving a commutative and associative operator
	(*, +, &, |, ^) may be rearranged arbitrarily, even in the
	presence of parentheses; to force a particular order of eval-
	uation, an explicit temporary must be used.

Thus, (2.0 + (3.0E30 + (-3.0E30))), currently may be reordered to:
((2.0 + 3.0E30) + (-3.0E30)).

bright@dataio.UUCP (Walter Bright) (08/12/85)

In article <610@cyb-eng.UUCP> bc@cyb-eng.UUCP (Bill Crews) writes:
>OK, I understand that order of evaluation is not guaranteed.  I assume that
>was done to make compilers easier to write.  Is there any other reason?  Does
>it really make compilers easier to write?

Yes, it really does. The reason is because C provides a very large number
of operators, and therefore an enormous number of combinations of them.
Code generators must be able to handle all this, and so to reduce the
complexity of the task, expressions are usually reorganized and manipulated
until they fit into a relatively small number of forms.

Also, allowing the compiler to reorder the expression within certain
limits allows it to do some optimizations. Most code is not affected by
order of evaluation, and C does allow an order to be forced (by assigning
an explicit temporary), so I think this is very reasonable.