Expression sequencing query

peters at cubsvax.UUCP peters at cubsvax.UUCP
Tue Oct 21 03:19:16 AEST 1986


In article <brl-smok.4642> 00R0DHESI%bsu.csnet at CSNET-RELAY.ARPA (Rahul Dhesi) writes:
>Peter S. Shenkin <cubsvax!peters at columbia.ARPA> writes:
><In article <umcp-cs.3769> chris at umcp-cs.UUCP (Chris Torek) writes:
><<	result = a + b;
><<	result += c;
><...
><...I wish there were some way of forcing order of execution, to 
><this extent anyway, within a line.
>
>Try this:
>     {int t = a + b; result = t + c;}  /* note:  force evaluation order */

So many have replied to me on this subject, both here and by netmail, that
I'd like to point out publicly what someone reminded me of by mail.

The draft ANSI standard specifies that unary "+" guarantees that a
parenthesized expression following it will be evaluated without interleaving
or other change of order;  thus, using the example previously bandied about, 
the usage:
	a = +( (b=1), b ) + +( (b=4), b ) + +( (b=16), b )
will guarantee that a is given a value of 21.

I rather like this;  it allows the code writer to specify where he thinks
order of evaluation is important, and allows the compiler to optimize
the order where it's not. 

We now have the following comparison:
	Fortran: parentheses specify both precedence of operators and
	   order of evaluation of operators with equal precedence, even
	   when the operators are commutative;
	C:  parentheses specify only precedence of operators.  Commutative
	   operators can have their arguments evaluated in any order, but
	   the default order, which will be compiler-dependent, can be
	   overridden with the unary "+".

You guys are giving us numerical folks less and less excuse to keep using
Fortran (break my heart)....  'Nuff said?

Peter S. Shenkin	 Columbia Univ. Biology Dept., NY, NY  10027
{philabs,rna}!cubsvax!peters		cubsvax!peters at columbia.ARPA



More information about the Comp.lang.c mailing list