473,385 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ternary expression validity

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.

Jan 12 '06 #1
5 2185
sl****************@hotmail.com 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

Jan 13 '06 #2
Robert Gamble wrote:
sl****************@hotmail.com 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/>
Jan 13 '06 #3
sl****************@hotmail.com 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.
Jan 13 '06 #4
sl****************@hotmail.com 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

Jan 13 '06 #5
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
Jan 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zipher | last post by:
It seems the debate over PEP 308 (if-then-else expression) occurred prior to the arrival of generator expressions. Mightn't this new latter syntax be the ticket to a "one obvious way" to write a...
6
by: Roger Leigh | last post by:
This code works (GCC 3.3.3): #include <iostream> int main() { bool a = true; std::cout << "True and false\n" << (a == true) ? 't' : 'f'; std::cout << "\n"; return 0;
6
by: glongword | last post by:
As the assert macro should evaluate to a void expression, it should not have an 'if statement' in its definition. Does this necessitate the existence of a ternary conditional operator? Is there a...
15
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
6
by: Robert Zurer | last post by:
In his paper on Coding Standard found on http://www.idesign.net/idesign/DesktopDefault.aspx Juval Lowy discourages the use of the ternary conditional operator with no specific reason given. ...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
20
by: Udo A. Steinberg | last post by:
Hi all, In a ternary statement such as: x = (cond ? a : b); it is obviously guaranteed that "x" will be equal to "a" only if the condition "cond" holds. Assuming that "a" is a memory location,...
5
by: PerlPhi | last post by:
hi,,, while ago i was wondering why do some programmers rarely uses the ternary operator. wherein it is less typing indeed. i believe in the classic virtue of Perl which is laziness. well let me show...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.