Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ternary expression validity

Reply
Thread Tools

ternary expression validity

 
 
slashdotcommacolon@hotmail.com
Guest
Posts: n/a
 
      01-12-2006
Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:

$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;

return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}

The error is 'ternary operation `?:` unterminated', however the GNU
compiler accepts it even in 'pedantic' mode. It's not a problem, as I
just rewrote the code, but I'm curious if this code (as i suspect)
looks acceptable to you language lawyers, if so I'll file a bug report
with the vendor.

Thanks guys.

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      01-12-2006
wrote:
> Hello, I've been asked to port some old code to a new platform. We have
> a vendor-supplied compiler that is supposed to be ansi compliant, but
> it refuses to compile the following code:
>
> $ cat test.c
> int main(int argc, char **argv)
> {
> static int a, b, c, d, e, f, g, h, i, j, k;
>
> return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
> }


The code above is valid and should compile properly. I happen to think
that the last statement is very poorly written though, I wouldn't want
to see the rest of the code written by the guy who came up with this
gem. This is the type of code that leads to bugs and maintenance
nightmares, especially if you forget that the tertiary operator is
right-to-left associative.

Robert Gamble

 
Reply With Quote
 
 
 
 
Chuck F.
Guest
Posts: n/a
 
      01-13-2006
Robert Gamble wrote:
> wrote:
>
>> Hello, I've been asked to port some old code to a new
>> platform. We have a vendor-supplied compiler that is supposed
>> to be ansi compliant, but it refuses to compile the following
>> code:
>>
>> $ cat test.c
>> int main(int argc, char **argv)
>> {
>> static int a, b, c, d, e, f, g, h, i, j, k;
>>
>> return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
>> }

>
> The code above is valid and should compile properly. I happen to
> think that the last statement is very poorly written though, I
> wouldn't want to see the rest of the code written by the guy who
> came up with this gem. This is the type of code that leads to
> bugs and maintenance nightmares, especially if you forget that
> the tertiary operator is right-to-left associative.


Yeah. What he said. In spades.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      01-13-2006
wrote:
> Hello, I've been asked to port some old code to a new platform. We have
> a vendor-supplied compiler that is supposed to be ansi compliant, but
> it refuses to compile the following code:
>
> $ cat test.c
> int main(int argc, char **argv)
> {
> static int a, b, c, d, e, f, g, h, i, j, k;
>
> return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
> }
>
> The error is 'ternary operation `?:` unterminated', however the GNU
> compiler accepts it even in 'pedantic' mode. It's not a problem, as I
> just rewrote the code, but I'm curious if this code (as i suspect)
> looks acceptable to you language lawyers, if so I'll file a bug report
> with the vendor.
>
> Thanks guys.
>

You should send this in as obfuscated code, anybody
writing that kind of code, should not be allowed near
a computer,and waste his employers money.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      01-13-2006
wrote:
> Hello, I've been asked to port some old code to a new platform. We have
> a vendor-supplied compiler that is supposed to be ansi compliant, but
> it refuses to compile the following code:
>
> $ cat test.c
> int main(int argc, char **argv)
> {
> static int a, b, c, d, e, f, g, h, i, j, k;
>
> return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
> }
>
> The error is 'ternary operation `?:` unterminated', however the GNU
> compiler accepts it even in 'pedantic' mode. It's not a problem, as I
> just rewrote the code, but I'm curious if this code (as i suspect)
> looks acceptable to you language lawyers, if so I'll file a bug report
> with the vendor.


I would hazzard a guess that the compiler writer is not aware that the
middle
term of the conditional operator is any other expression...

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

In other words...

a ? b, c : d

....is equivalent to...

a ? (b, c) : d

--
Peter

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      01-13-2006
Robert Gamble wrote:

> tertiary operator


There are unary, binary, and ternary operators,
according to the number of operands of the operator;
rather than primary, secondary, and tertiary.

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
ternary trouble in datalist/repeater Darren ASP .Net 2 08-13-2004 04:12 PM
how to check validity in expression template Rex_chaos C++ 3 10-28-2003 10:21 AM
ternary operator error Jacob Java 12 07-02-2003 07:12 PM



Advertisments