short circuit evaluation

Root root at sdd.UUCP
Sun Mar 8 03:51:58 AEST 1987


In article <609 at viper.UUCP> john at viper.UUCP (John Stanley) writes:
>
>If you have three expressions:
>	1:  a+b+c
>	2: (a+b)+c
>	3:  a+(b+c)
>all three of them should not be evaluated the same way.  Greg implys that
>they should be.  This is not so.  When an equation contains '(' and ')'
>it intentionaly (and explicitly) defines the parse tree structure that will
>result.  The statement "redundant ()'s grow in C like mushrooms" may be true,
>but it doesn't give anyone the right to arbitrarily ignore explicit cues
>to the compiler.  When I don't care, I don't use them.  When I do, I do
>so for a reason..........

	NO!!!!  The new DRAFT ANSI standard explicitly says:

	"An expression involving more than one occurance of the same cummutative
	and associtive binary operator (*, +, &, ^, |) may be regrouped 
	arbitrarily, EVEN IN THE PRESENCE OF PARENTHESES, provided the types of
	the operands or of the results are not changed by this regrouping.  To
	force a particular grouping of operations, either the value of the
	expression to be grouped may be explicitly assigned to an object, or
	grouping parentheses may be preceded by a unary plus operator."


Therefore if one desires to explicitly specify an order, in the new ANSI
standard one can use a unary plus operator as follows:

	1:	+(a + b) + c
	2:	a + (+(a + b))

This should guarantee the evaluation order!

									Daniel Corbett
									Software Design & Development Corp
									360 Mobil, Suite 103
									Camarillo, CA 93010



More information about the Comp.lang.c mailing list