post-decrement quirk?

Stephen Clamage steve at taumet.com
Wed Jun 19 02:50:09 AEST 1991


rgm at ocf.berkeley.edu (Rob Menke) writes:

>In article <1991Jun17.230838.9628 at auto-trol.com> julbro at auto-trol.com
>(Julie Brown) writes:

>   Can anyone explain to me why the following does not work 
>	q = q--;

>It's quite simple:  set-value ('=') has a lower precedence than
>post-decrement.  So, the expression on the RHS is evaluated (it equals
>'q'), q is decremented, then q is set to the stored value from the
>RHS.  Net result: no decrement.

This is not a correct explanation.  We have been through endless comments
on another thread, the evaluation of
	if( (i = 1) == (i = 2) )
and the situation here is the same.  In the expression
	q = q--
variable q is assigned a value in two places with no intervening sequence
point.  The result of the double assignment between sequence points is
undefined.  A compiler could reasonably evaluate the expression as if
you had written
	q = q; q--; 
or as if you had written
	tmp = q; q--; q = tmp;
In fact, since the result is specifically "undefined" (a technical term
in ANSI C), the compiler is free to do whatever it likes, such as send
you mail at run time telling you not to modify a variable twice between
sequence points.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list