home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!metro!basser.cs.su.oz.au!swift!sour.sw.oz.au!pod
- From: pod@sour.sw.oz.au (Paul O'Donnell)
- Subject: Re: a ? b : c = d; /* legal ? */
- Organization: Softway Pty Ltd
- Date: 29 Dec 92 23:56:55 GMT
- Message-ID: <pod.725673415@sour.sw.oz.au>
- References: <BzDA4u.459@cdsmn.mn.org>
- Keywords: interpretation, conditional operator, language issue
- Sender: news@softway.sw.oz.au (Usenet)
- Lines: 68
-
- In <BzDA4u.459@cdsmn.mn.org> wells@cdsmn.mn.org (Rich Wells) writes:
-
- [ Deleted (correct) discussion that a ? b : c = d; is equivalent to
- (a ? b : c) = d; and is bogus. ]
-
- >***** 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.
-
- This is correct behaviour.
-
- >***** 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.
-
- Yuk, this is wrong, wrong, wrong as you correctly surmise.
-
- >***** Turbo C++ (3.0):
-
- >The statement is interpreted 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?)
-
- Surely you mean:
-
- if (a)
- b = d;
- else
- c = d;
-
- Gcc does this too. If you use the -pedantic flag, it warns you that
- that ANSI disallows it, but does it anyway. I don't know what is
- correct for C++. It does seem to be a reasonable extension.
-
- >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.
-
- Well I hope he didn't take the job, because they were wrong.
-
- >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.
-
- You did fine.
-
- --
- Paul O'Donnell - Softway Pty Ltd |"You're just saying that because
- Internet: pod@softway.sw.oz.au |you don't understand art."
- Phone: +61 2 698-2322 Fax: +61 2 699-9174| - Jeremy Fitzhardinge
-