Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!bellcore!att!cbnewsl!dfp From: dfp@cbnewsl.ATT.COM (david.f.prosser) Newsgroups: comp.lang.c Subject: Re: still problems with ?: Message-ID: <809@cbnewsl.ATT.COM> Date: 14 Jun 89 19:48:35 GMT References: <10387@smoke.BRL.MIL> <568@lakart.UUCP> Reply-To: dfp@cbnewsl.ATT.COM (david.f.prosser) Organization: AT&T Bell Laboratories Lines: 49 In article <568@lakart.UUCP> dg@lakart.UUCP (David Goodenough) writes: ]gwyn@smoke.BRL.MIL (Doug Gwyn) sez: ]> In article <4675@alvin.mcnc.org> spl@mcnc.org.UUCP (Steve Lamont) writes: ]>>>((a = *p++) && e2 ) || (!a && e3) ]>>Is right to left evaluation mandated in this case? It seems to me that ]>>I've been bitten by things like this before, either by non-standards ]>>conforming compilers, ambiguous definition in the standard, my own ]>>stupidity, or all of the above. In any case, it seems that defensive ]>>programming might dictate something like ... ]> ]> There has never been any ambiguity about the correct sequence of ]> operations during evaluation of such an expression (except when ]> `p' is used in `e2' or `e3'). ] ]Huh??? - if I use p in e2 or e3, my compiler had better use the value ]_AFTER_ the *p++, otherwise I'm going to ask for my money back. In the ]above code fragment if _ANYTHING_ is executed before the a = *p++ then ]the compiler is broken. The same applies to: ] ] if ((a = *p++) ? e2 : e3) ] ]The a = *p++ part has to be done first so that the compiler knows which ]of e2 and e3 to do. Of course if your going to tell me that the standard ]says that the a = *p and the p++ may happen at different times (i.e. the ]p++ after e2), then I'm going to say the standard is broken. ]-- ] dg@lakart.UUCP - David Goodenough +---+ ] IHS | +-+-+ ] ....... !harvard!xait!lakart!dg +-+-+ | ]AKA: dg%lakart.uucp@xait.xerox.com +---+ David is correct: There are sequence points at the appropriate operators in these expressions. Because all pending updates must occur between the previous and the next sequence point, the update to p in each of the following expressions must occur prior to the evaluation of e2 or e3: (a = *p++) ? e2 : e3 /* ^ sequence point */ ((a = *p++) && e2) || (!a && e3) /* ^ ^ ^ sequence points */ But in ((a = *p++) & e2) | (!a & e3) there is no guarantee about the update of p with respect to the evaluation of the rest of the expression. Dave Prosser ...not an official X3J11 answer...