post-decrement quirk?

Rob Menke rgm at ocf.berkeley.edu
Tue Jun 18 12:45:32 AEST 1991


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.

You're better off with:
	q = (q > 0) ? q - 1 : 0;
--
"It is a pity that you Autobots die so	|  Robert Menke
 easily... otherwise one might get a	|    rgm at OCF.berkeley.edu
 sense of satisfaction..."		|    ...!ucbvax!OCF!rgm



More information about the Comp.lang.c mailing list