C compiler implements wrong semantics

hans at erisun.UUCP hans at erisun.UUCP
Tue Feb 4 22:13:27 AEST 1986


This has no doubt been multiply reiterated over the years, but here goes
again:

	The result of the statement 
	b = ( a++ + a );
	is not defined by the semantics of C.
	Evaluation of subexpressions may be performed in any
	order and, specifically, code which depends on
	subexpression evaluation order is erroneous.
	This is one trait C shares with most sequential
	assignment based procedural languages.

As an aside, there is an operator, ',' , which defines
evaluation order and not much else, and there are
the && and || operators, of course, but these destroy arithmetic
values in their course of duty.
The above statement could be written as
	b = ( ( b = a++ ), b += a  )
to produce one particular evaluation order, 
or
	b = ( ( b = a ), b += a++ )
for the other order, but neither appears to have any advantages
compared to their sequential statement forms,

	b  = a;				b  = a;
	b += ++a;			b += a++;

which are semantically indisputable.


-- 
 Two's complement, but three's an int.
Hans Albertsson EIS, USENET/uucp: {decvax,philabs}!mcvax!enea!erix!erisun!hans



More information about the Comp.unix.wizards mailing list