Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uunet!world!burley From: burley@world.std.com (James C Burley) Newsgroups: comp.lang.c Subject: Sequence Points, Swapping Vars (Was Re: Is...wrong with the compiler?) Message-ID: Date: 29 Sep 90 08:57:36 GMT References: <884@gtenmc.UUCP> <1990Sep26.175948.8232@zoo.toronto.edu> <189@thor.UUCP> Sender: burley@world.std.com (James C Burley) Organization: The World Lines: 38 In-Reply-To: scjones@thor.UUCP's message of 28 Sep 90 21:53:35 GMT In article <189@thor.UUCP> scjones@thor.UUCP (Larry Jones) writes: In article , burley@world.std.com (James C Burley) writes: > > a = ( int )(( unsigned ) ( a = ~0 ) >> 1 )); > > Hmm, I thought it was ok -- doesn't "=" serve as a "sequence point" or > some such thing, and since the lhs of the outer = is so simple it can't > be "evaluated" before the rhs has been, and the rhs contains the other > =, one is guaranteed the latter = is performed before the former, right? No, '=' is not a sequence point. The question in this case is not when the lhs gets evaluated, but when the actual storage of the rhs into the lhs is done. A compiler would be completely within its rights to store the final result into a and then overwrite it with ~0. ---- Larry Jones UUCP: uunet!sdrc!thor!scjones SDRC scjones@thor.UUCP 2000 Eastman Dr. BIX: ltl Milford, OH 45150-2789 AT&T: (513) 576-2070 Yep, we'd probably be dead by now if it wasn't for Twinkies. -- Calvin I was wrong, indeed: = is not a sequence point, and thanks to several people who pointed it out. However, I did finally look it up in the standard and now I'm confused. Apparently, other assignment operators (such as &=) are not sequence points, either. The reason this confuses me is the other thread(s) I've seen posting solutions such as "a^=b^=a^=b" (or some such thing) for the problem of swapping integer variables. If ^= also is not a sequence point, isn't it valid for a conforming C compiler to write into a the result of XORing the original values of a and b, instead of the final value (the original value of b)? It was this discussion I was thinking about when I originally posted my mistaken impression that = is a sequence point. Or is "a^=b^=a^=b" a nonstandard solution (in addition, perhaps, to being an inappropriate one)? James Craig Burley, Software Craftsperson burley@world.std.com