home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18658 < prev    next >
Encoding:
Internet Message Format  |  1992-12-21  |  1.3 KB

  1. Path: sparky!uunet!cs.utexas.edu!asuvax!ncar!noao!arizona!optima.UUCP
  2. From: dwebster@optima.UUCP (Dave E. Webster Jr.)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: a ? b : c = d; /* legal ? */
  5. Message-ID: <28578@optima.cs.arizona.edu>
  6. Date: 19 Dec 92 00:36:06 GMT
  7. References: <BzDA4u.459@cdsmn.mn.org>
  8. Sender: news@cs.arizona.edu
  9. Lines: 38
  10.  
  11. Rich Wells asks:
  12.  
  13. > Is the statement:
  14. >     a ? b : c = d;
  15. > where a, b, c, and d are all integers, a valid C expression?
  16.   ...
  17. > ***** Microsoft C (7.0 and 5.1): The statement does not compile.
  18.   ...
  19. > ***** Turbo C++ (3.0): The statement is interpreted as:
  20. >     if (a)
  21. >         a = d;
  22. >     else
  23. >         b = d;
  24.  
  25. Rich,
  26.  
  27. Assuming that the TC++ implementation should actually read as:
  28.  
  29.      if (a) b = d; else c = d;
  30.             ^           ^
  31.  
  32. then both of the above compilers are correct.  The Microsoft compiler
  33. flags an error because the result of the trinary conditional in C is
  34. an rvalue and cannot be the target of an assignment.  C++ changed the
  35. rules by defining the result of a trinary conditional as an lvalue of
  36. type T if (and only if) both the second and third arguments are
  37. themselves lvalues of a common type (cf arm 5.16).
  38.  
  39.   I have not seen this feature used much in C++ but other languages
  40. with similar semantics make great use of this and similar semantics
  41. (eg: Icon).
  42.  
  43. Merry XMAS,
  44.  
  45. Dave
  46.