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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!munnari.oz.au!metro!basser.cs.su.oz.au!swift!sour.sw.oz.au!pod
  3. From: pod@sour.sw.oz.au (Paul O'Donnell)
  4. Subject: Re: a ? b : c = d; /* legal ? */
  5. Organization: Softway Pty Ltd
  6. Date: 29 Dec 92 23:56:55 GMT
  7. Message-ID: <pod.725673415@sour.sw.oz.au>
  8. References: <BzDA4u.459@cdsmn.mn.org>
  9. Keywords: interpretation, conditional operator, language issue
  10. Sender: news@softway.sw.oz.au (Usenet)
  11. Lines: 68
  12.  
  13. In <BzDA4u.459@cdsmn.mn.org> wells@cdsmn.mn.org (Rich Wells) writes:
  14.  
  15. [ Deleted (correct) discussion that a ? b : c = d; is equivalent to
  16.   (a ? b : c) = d; and is bogus. ]
  17.  
  18. >***** Microsoft C (7.0 and 5.1):
  19.  
  20. >The statement does not compile.  The compiler complains
  21. >(rightly, I think) that the ?: does not yield an lvalue,
  22. >and so the assignment is invalid.
  23.  
  24. This is correct behaviour.
  25.  
  26. >***** Metaware HighC (version 3.01 for the 386):
  27.  
  28. >The statement is interpreted as:
  29.  
  30. >    if (a)
  31. >        b;
  32. >    else
  33. >        c = d;
  34.  
  35. >which, it seems to me, ignores precedence rules.
  36.  
  37. Yuk, this is wrong, wrong, wrong as you correctly surmise.
  38.  
  39. >***** Turbo C++ (3.0):
  40.  
  41. >The statement is interpreted as:
  42.  
  43. >    if (a)
  44. >        a = d;
  45. >    else
  46. >        b = d;
  47.  
  48. >In other words, it allows the ?: operator to yield an
  49. >lvalue, and uses that for the assignment.  (NOTE: TC++
  50. >is a C++ compiler; does this change the answer?)
  51.  
  52. Surely you mean:
  53.  
  54.     if (a)
  55.         b = d;
  56.     else
  57.         c = d;
  58.  
  59. Gcc does this too.  If you use the -pedantic flag, it warns you that
  60. that ANSI disallows it, but does it anyway.  I don't know what is
  61. correct for C++.  It does seem to be a reasonable extension.
  62.  
  63. >This came from a friend of a friend
  64. >(perhaps I should post to alt.folklore.computers?) who claims
  65. >he was asked this on an employment exam, and was told that
  66. >the Turbo C++ interpretation is correct.
  67.  
  68. Well I hope he didn't take the job, because they were wrong.
  69.  
  70. >BTW: if I have used the words 'statement', 'expression' and
  71. >'lvalue' wrong, forgive me; I'm not an experienced language
  72. >lawyer.  However, please correct me if I used them wrong;
  73. >inquiring minds want to know.
  74.  
  75. You did fine.
  76.  
  77. -- 
  78. Paul O'Donnell - Softway Pty Ltd         |"You're just saying that because
  79. Internet: pod@softway.sw.oz.au           |you don't understand art."
  80. Phone: +61 2 698-2322 Fax: +61 2 699-9174|     - Jeremy Fitzhardinge
  81.