home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!umn.edu!mmm.serc.3m.com!pwcs!cdsmn!wells
- From: wells@cdsmn.mn.org (Rich Wells)
- Subject: a ? b : c = d; /* legal ? */
- Message-ID: <BzDA4u.459@cdsmn.mn.org>
- Summary: interpretation question re conditional operator
- Keywords: interpretation, conditional operator, language issue
- Organization: Dicomed, Inc
- X-Newsreader: TIN [version 1.1 PL6]
- Date: Wed, 16 Dec 1992 19:29:17 GMT
- Lines: 66
-
- Is the statement:
-
- a ? b : c = d;
-
- where a, b, c, and d are all integers, a valid C expression?
- I suspect not, since precedence rules determine that the
- statement is equivalent to
-
- (a ? b : c) = d;
-
- and since the ?: operator does not yield an lvalue, the
- expression is flawed.
-
- The above argument is based on the book _ANSI C: A Lexical
- Guide_, a not entirely unimpeachable source, so I thought
- I'd consult the net.wisdom for the correct answer.
-
- Also, the three compilers I have experimented with each have
- different interpretations. To wit:
-
- ***** Microsoft C (7.0 and 5.1):
-
- The statement does not compile. The compiler complains
- (rightly, I think) that the ?: does not yield an lvalue,
- and so the assignment is invalid.
-
- ***** Metaware HighC (version 3.01 for the 386):
-
- The statement is interpreted as:
-
- if (a)
- b;
- else
- c = d;
-
- which, it seems to me, ignores precedence rules.
-
- ***** Turbo C++ (3.0):
-
- The statement is interpretted as:
-
- if (a)
- a = d;
- else
- b = d;
-
- In other words, it allows the ?: operator to yield an
- lvalue, and uses that for the assignment. (NOTE: TC++
- is a C++ compiler; does this change the answer?)
-
- *****
-
- So that I don't have to go out and buy an asbestos suit, I
- want to make it very clear that I would never even consider
- writing code like this. This came from a friend of a friend
- (perhaps I should post to alt.folklore.computers?) who claims
- he was asked this on an employment exam, and was told that
- the Turbo C++ interpretation is correct.
-
- BTW: if I have used the words 'statement', 'expression' and
- 'lvalue' wrong, forgive me; I'm not an experienced language
- lawyer. However, please correct me if I used them wrong;
- inquiring minds want to know.
- --
-
- Richard Wells wells@cdsmn.mn.org or ...!tcnet!cdsmn!wells
-