home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!darwin.sura.net!spool.mu.edu!umn.edu!mmm.serc.3m.com!pwcs!cdsmn!wells
- From: wells@cdsmn.mn.org (Rich Wells)
- Subject: Re: a ? b : c = d; /* legal ? */
- Message-ID: <BzM97n.LuE@cdsmn.mn.org>
- Organization: Dicomed, Inc
- X-Newsreader: TIN [version 1.1 PL6]
- References: <28578@optima.cs.arizona.edu>
- Date: Mon, 21 Dec 1992 15:47:46 GMT
- Lines: 47
-
- Dave E. Webster Jr. (dwebster@optima.UUCP) wrote:
- : Rich Wells (that's me) asks:
- :
- : > Is the statement:
- : >
- : > a ? b : c = d;
- : >
- : > where a, b, c, and d are all integers, a valid C expression?
- : ...
- : > ***** Microsoft C (7.0 and 5.1): The statement does not compile.
- : ...
- : > ***** Turbo C++ (3.0): The statement is interpreted as:
- : >
- : > if (a)
- : > a = d;
- : > else
- : > b = d;
- :
- : Rich,
- :
- : Assuming that the TC++ implementation should actually read as:
- :
- : if (a) b = d; else c = d;
- : ^ ^
-
- You are, of course right. I even had a second person proof-read this
- before sending it out. Sorry about the mistake. How embarrassing.
-
- : then both of the above compilers are correct. The Microsoft compiler
- : flags an error because the result of the trinary conditional in C is
- : an rvalue and cannot be the target of an assignment. C++ changed the
- : rules by defining the result of a trinary conditional as an lvalue of
- : type T if (and only if) both the second and third arguments are
- : themselves lvalues of a common type (cf arm 5.16).
-
- Thanks for the answer, Dave.
-
- I went back and tested Microsoft C 7.0 again, this time using
- .cpp as the file extension. This time it compiled and ran
- as expected for a C++ compiler. I haven't had time to check to
- if TC++'s behavior changes accordingly; although I'm relatively
- certain I used a .c extension to get the C++ behavior, I'm not
- sure if TC uses the file extension or some internal option setting
- to determine whether to allow C++ code or just C code.
- --
-
- Richard Wells wells@cdsmn.mn.org or ...!tcnet!cdsmn!wells
-