Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!bellcore!uunet!olivea!mintaka!ogicse!cvedc!gssc!timr From: timr@gssc.UUCP (Tim Roberts) Newsgroups: comp.lang.c Subject: Re: post-decrement quirk? Message-ID: <6682@gssc.UUCP> Date: 19 Jun 91 15:39:53 GMT References: <1991Jun17.230838.9628@auto-trol.com> Distribution: usa Organization: Graphic Software Systems, Inc. Beaverton, Or. Lines: 33 In article rgm@ocf.berkeley.edu (Rob Menke) writes: >In article <1991Jun17.230838.9628@auto-trol.com> julbro@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@gssc.gss.com Tim N Roberts, CCP Graphic Software Systems Beaverton, OR This is a very long palindrome. .emordnilap gnol yrev a si sihT