Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!samsung!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: post-decrement quirk? Message-ID: <771@taumet.com> Date: 18 Jun 91 16:50:09 GMT References: <1991Jun17.230838.9628@auto-trol.com> Distribution: usa Organization: Taumetric Corporation, San Diego Lines: 32 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 > 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@taumet.com