473,786 Members | 2,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2201
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.c om, 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
1861
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 ternary expression in python? >>> (foo(i) if i==42 else bar(i)) # i==42 ? foo(i) : bar(i) zipher
6
2263
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
2766
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 way assert.h can be implemented without using it? Are these decisions related in anyway?
15
2808
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
6
2990
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. I can understand that complicated code would be much less readable using nested operators, but aside from that is there another reason? -- something going on under the hood perhaps which make it inadvisable to use?
48
2759
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 suggestions?
20
2163
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, is it also guaranteed that "a" will not be accessed in memory if the condition does not hold? Or, in other words, is a compiler allowed to speculatively fetch a and b from memory and then assign one of them to x based on the condition?
5
3567
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 you something first before i go to my delimma. codes as follows using Mrs. If Else: #!perl/bin/perl use strict; print "Are you sure you want to quit ('yes' or any key for 'no'): "; chomp($_ = <STDIN>);
162
10301
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 prefix them with 2 underscores, but I hate prefixing my vars, I'd rather add a keyword before it. Python advertises himself as a full OOP language, but why does it miss one of the basic principles of OOP? Will it ever be added to python?
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.