home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18795 < prev    next >
Encoding:
Internet Message Format  |  1992-12-22  |  2.2 KB

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