home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18758 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.0 KB  |  59 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!umn.edu!mmm.serc.3m.com!pwcs!cdsmn!wells
  3. From: wells@cdsmn.mn.org (Rich Wells)
  4. Subject: Re: a ? b : c = d; /* legal ? */
  5. Message-ID: <BzM97n.LuE@cdsmn.mn.org>
  6. Organization: Dicomed, Inc
  7. X-Newsreader: TIN [version 1.1 PL6]
  8. References: <28578@optima.cs.arizona.edu>
  9. Date: Mon, 21 Dec 1992 15:47:46 GMT
  10. Lines: 47
  11.  
  12. Dave E. Webster Jr. (dwebster@optima.UUCP) wrote:
  13. : Rich Wells (that's me) asks:
  14. : > Is the statement:
  15. : > 
  16. : >     a ? b : c = d;
  17. : > 
  18. : > where a, b, c, and d are all integers, a valid C expression?
  19. :   ...
  20. : > ***** Microsoft C (7.0 and 5.1): The statement does not compile.
  21. :   ...
  22. : > ***** Turbo C++ (3.0): The statement is interpreted as:
  23. : > 
  24. : >     if (a)
  25. : >         a = d;
  26. : >     else
  27. : >         b = d;
  28. : Rich,
  29. : Assuming that the TC++ implementation should actually read as:
  30. :      if (a) b = d; else c = d;
  31. :             ^           ^
  32.  
  33. You are, of course right.  I even had a second person proof-read this
  34. before sending it out.  Sorry about the mistake.  How embarrassing.
  35.  
  36. : then both of the above compilers are correct.  The Microsoft compiler
  37. : flags an error because the result of the trinary conditional in C is
  38. : an rvalue and cannot be the target of an assignment.  C++ changed the
  39. : rules by defining the result of a trinary conditional as an lvalue of
  40. : type T if (and only if) both the second and third arguments are
  41. : themselves lvalues of a common type (cf arm 5.16).
  42.  
  43. Thanks for the answer, Dave.
  44.  
  45. I went back and tested Microsoft C 7.0 again, this time using
  46. .cpp as the file extension.  This time it compiled and ran
  47. as expected for a C++ compiler.  I haven't had time to check to
  48. if TC++'s behavior changes accordingly; although I'm relatively
  49. certain I used a .c extension to get the C++ behavior, I'm not
  50. sure if TC uses the file extension or some internal option setting
  51. to determine whether to allow C++ code or just C code.
  52. -- 
  53.  
  54. Richard Wells  wells@cdsmn.mn.org  or  ...!tcnet!cdsmn!wells
  55.