Expression sequencing query

Jason Zions jason at hpcnoe.UUCP
Tue Sep 23 09:15:50 AEST 1986


> / tomc at oakhill.UUCP (Tom Cunningham) /  4:16 pm  Sep  5, 1986 /
> Sorry if this topic has been overly exercised already.  In the following
> code fragment:
> 
> 	/* a = b + b + b */
> 	a = ((b=1),b) + ((b=2),b) + ((b=3),b)
> 
[ Wants result to be 6; his micro compiler does that, his big system compiler
  produces 9. Doesn't know if it's a bug. ]
> Tom Cunningham     "Good, fast, cheap -- select two."
> UUCP:  {ihnp4,seismo,ctvax,gatech}!ut-sally!oakhill!tomc

Sorry. K&R does guarantee that the left side of a comma operator will be
evaluated sometime before the right side of the operator, but does not say
ANYTHING about interleaving evaluations of other expressions.

This was covered about a month ago, but not quite in the same context. The
upshot of it all was that interleaving of expression evaluation is indeed
legal, and some compilers (4.2BSD, I believe, is a notorious example) are
known to do so.

"Doctor, Doctor, I get bit every time I use multiple side-effects in the
 same statement!"

"So don't do that!"
--
This is not an official statement of Hewlett-Packard Corp., and does not 
necessarily reflect the views of HP. It is provided completely without warranty
of any kind. Lawyers take 3d10 damage and roll a saving throw vs. ego attack.

Jason Zions				Hewlett-Packard
Colorado Networks Division		3404 E. Harmony Road
Mail Stop 102				Ft. Collins, CO  80525
	{ihnp4,seismo,hplabs,gatech}!hpfcdc!hpcnoe!jason



More information about the Comp.lang.c mailing list