🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #930

Order of Evaluation

#930
From: Christopher Gould
Date: Sat, 18 Nov 1989 01:20
33 lines
2042 bytes
	This is a question about the justification for pANS _not_
making a change from K&R-1:

	Why is the order of evaluation of subexpressions undefined?
This applies either for subexpressions of most binary operators, or as
arguments in function calls.  Specifically, in the following example,
it is presently undefined as to whether the loop adds the even elements
of a[] with the odd elements of b[], or vice versa:

		int a[SIZE], b[SIZE], i, j;
		for ( i=0 ; i < SIZE-2 ;    )
			j = a[i++] + b[i++];

Of course, this loop could easily be rewritten to specify which one is
desired by moving the i++ into the loop statement, and giving an offset
to the appropriate array index.  This question is not about style.

	What I am asking is, what would go wrong if a new Standard were
to say, for example, you always evaluate left to right.  Then we would
get a[0] + b[1] + a[2] + ....   Since the order of evaluation has never
been guaranteed, a new Standard establishing an order of evaluation
(whatever choice one might make) could not break any currently working
code.  Furthermore, it would reduce the number of ambiguous statements
legally constructed within the language.  (Whether this is in itself a
"good thing" is perhaps the real issue.)

	Incidentally, the above example is a case where parentheses
cannot be used to force the order of evaluation.
_________________

Christopher M. Gould - Univ of Southern Calif - Dept of Physics   (213)743-8521
Internet: gould@usc.edu    Bitnet: gould@uscvm    UUCP: uunet!usc!physics!gould
Technical Details
Message-ID: <21265@usc.edu>
Path: arc1-ca.pugleaf.net!utzoo!yunexus!ists!jarvis.csri.toronto.edu!mailrus!wuarchive!rex!samsung!usc!physics.usc.edu!gould

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!yunexus!ists!jarvis.csri.toronto.edu!mailrus!wuarchive!rex!samsung!usc!physics.usc.edu!gould
From: gould@physics.usc.edu (Christopher Gould)
Newsgroups: comp.std.c
Subject: Order of Evaluation
Message-ID: <21265@usc.edu>
Date: 18 Nov 89 01:20:16 GMT
Article-I.D.: usc.21265
Sender: news@usc.edu
Reply-To: gould@physics.usc.edu (Christopher M. Gould)
Followup-To: comp.std.c
Organization: University of Southern California, Los Angeles, CA
Lines: 33