home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!world!ksr!jfw
- From: jfw@ksr.com (John F. Woods)
- Newsgroups: comp.lang.c
- Subject: Re: Question to test general C knowledge
- Message-ID: <20356@ksr.com>
- Date: 22 Dec 92 15:05:02 EST
- References: <1992Dec16.192652.65097@ns1.cc.lehigh.edu> <1992Dec20.230703.10039@thunder.mcrcim.mcgill.edu>
- Sender: news@ksr.com
- Lines: 39
-
- mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes:
- >In article <1992Dec16.192652.65097@ns1.cc.lehigh.edu>, dsbb@ns1.cc.lehigh.edu (D. SPENCER BEECHER) writes:
- >> D. SPENCER BEECHER) writes:
- >>>> varname%3
- >>>> varname&0x02
- >>> Well, [...]
- >> x&0x02 may be represented with modulo as:
- >> x%=4,x>1?x:0;
- >> though this changes the value of x, where x&0x02 makes no assignment.
- >It also doesn't work. For example, if x holds 7, x&2 is 2 whereas your
- >expression produces 3. (And, as you note, changes x.)
- >I'm also not sure whether , or ?: has higher precedence. For that to
- >work correctly, it has to be interpreted as (x%=4),((x>1)?x:0), but I'm
- >not sure it wouldn't be (((x%=4),x)>1)?x:0 instead, which I *think*
- >produces undefined behavior.
-
- If C operators had precedence, ?: would have higher precedence, and comma
- would have the lowest precedence of all. Since they don't :-), one simply
- needs to note that an "expression" may be "expression , assignment-expression",
- and an "assignment-expression" may be a "conditional-expression", which
- of course may be "logical-OR-expression ? expression : conditional-expression",
- which amounts to the same thing :-).
-
- (((x %=4),x)>1)?x:0 should be well-defined, however: there is a sequence
- point at the comma and at the question mark ("The first operand is evaluated;
- there is a sequence point after its evaluation." [1]), and the order of those
- sequence points is even well-defined.[2] So, the modification of x is safely
- interred before its value is queried again, and would have been even if the
- expression were written
-
- (x %= 4) > 1 ? x : 0;
-
- [1] Well, it doesn't quite explicitly say that sequence point *preceeds* the
- evaluation of the second or third expression, but surely that is the intent
- (otherwise the sequence point could have been specified as being for the whole
- conditional expression).
-
- [2] Yes, folks, I'm arguing that something *is* defined by the standard. Now
- ain't THAT a shock :-) ?
-