post-decrement quirk?

Tim Roberts timr at gssc.UUCP
Thu Jun 20 01:39:53 AEST 1991


In article <RGM.91Jun17184532 at avalanche.Berkeley.EDU> 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 
>   (at least on the Sun):
>
>	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.

Although we need to be aware that this result is simply coincidence, and
some other compiler might produce different results.  Following the lengthy
and rather heated debate about the results of undefined statements in ANSI
C, I hope all you viewers out there at home recognized that the results of

	q = q--;

are UNDEFINED, because, like the statement which started this whole mess, this 
statement modifies a variable twice without an intervening sequence point. 

>You're better off with:
>	q = (q > 0) ? q - 1 : 0;

Correct.  The results of this are well-defined.
-- 
timr at gssc.gss.com	Tim N Roberts, CCP	Graphic Software Systems
						Beaverton, OR

This is a very long palindrome. .emordnilap gnol yrev a si sihT



More information about the Comp.lang.c mailing list