473,394 Members | 1,694 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,394 software developers and data experts.

abuse of conditional operators (fwd)


A friend of mine asked me a C question that I couldn't answer. Could
you guys weigh in?

---------- Forwarded message ----------
Date: Sun, 21 Nov 2004 21:08:27 -0500
From: Rick Kennell <ri**@kennell.org>
To: no*****@purdue.edu
Subject: abuse of conditional operators

I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if 'a' is non-zero, assign 'd' to 'b', otherwise assign 'd' to 'c'.
Technically, there's no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue. GCC handles it and modern versions at least warn that this it's
deprecated behavior. TCC silently bungles it. I don't know about other
compilers.
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;

Specifically, the code was:

int tickadj = 500/HZ ? : 1;

which sets tickadj either to 500/HZ or to 1 if HZ is larger than 500.

Effectively, this means that

a = b ? : c;

is equivalent to

a = b ? b : c;

Neither the original K&R nor my copy of X3J11 say nothing about this.
There is a note about it in the GCC documentation

http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals

but it doesn't say anything about C99. Am I correct in assuming it's
just a GCCism?

Rick

Nov 14 '05 #1
11 1687
Dan Noland <no******@cs.purdue.edu> wrote:
I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if 'a' is non-zero, assign 'd' to 'b', otherwise assign 'd' to 'c'.
Technically, there's no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue.
<http://www.eskimo.com/~scs/C-faq/q3.16.html>
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;


That's a syntax error. Possibly Ganuck allows it; ISO does not, neither
C89 nor C99.

Richard
Nov 14 '05 #2
Richard Bos wrote:
Dan Noland <no******@cs.purdue.edu> wrote:

I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if 'a' is non-zero, assign 'd' to 'b', otherwise assign 'd' to 'c'.
Technically, there's no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue.

<http://www.eskimo.com/~scs/C-faq/q3.16.html>
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;

That's a syntax error. Possibly Ganuck allows it; ISO does not, neither
C89 nor C99.

Ganuck (LOL:-) allows it. from
http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals

5.8 Conditionals with Omitted Operands

The middle operand in a conditional expression may be omitted. Then if
the first operand is nonzero, its value is the value of the conditional
expression.

Therefore, the expression

x ? : y

has the value of x if that is nonzero; otherwise, the value of y.

This example is perfectly equivalent to

x ? x : y

In this simple case, the ability to omit the middle operand is not
especially useful. When it becomes useful is when the first operand
does, or may (if it is a macro argument), contain a side effect. Then
repeating the operand in the middle would perform the side effect twice.
Omitting the middle operand uses the value already computed without the
undesirable effects of recomputing it.
[END QUOTE]

Why they chose to "extend" the language like this is beyond me, since it
should be very easy to use standard C and still avoid any recomputations.

Bjørn

Nov 14 '05 #3
Dan Noland schrieb:
Specifically, the code was:

int tickadj = 500/HZ ? : 1;

which sets tickadj either to 500/HZ or to 1 if HZ is larger than 500.


Huh, I would rather guess the purpose is to avoid a division by zero!?

So it should be read as: 500/(HZ ? : 1)

Sascha
Nov 14 '05 #4
Sascha Springer <ss*******@convision.com> wrote:
Dan Noland schrieb:
Specifically, the code was:

int tickadj = 500/HZ ? : 1;

which sets tickadj either to 500/HZ or to 1 if HZ is larger than 500.


Huh, I would rather guess the purpose is to avoid a division by zero!?

So it should be read as: 500/(HZ ? : 1)


Except that / binds more strongly than ?:, so the correct reading really
is (500/HZ)? : 1;
I suspect your reading, while not what it says, is what was intended.
Still a syntax error, though.

(And I note that the Ganuck construction X? : Y, for any X and Y
possibly including side-effects, can be trivially written in real C as
(tmp=X)? tmp: Y.)

Richard
Nov 14 '05 #5
In article <YI*******************@juliett.dax.net>,
=?ISO-8859-1?Q?Bj=F8rn_Augestad?= <bo*@metasystems.no> wrote:
....
Why they chose to "extend" the language like this is beyond me, since it
should be very easy to use standard C and still avoid any recomputations.


I applaud GCC and it's very successful efforts to embrace and extend
minimalist^Wstandard C.

Ever hear of this company called, er, what was that now, oh, yeah,
Microsoft? They wrote the book on e&e.

Nov 14 '05 #6
Dan Noland wrote:

A friend of mine asked me a C question that I couldn't answer. Could
you guys weigh in?

---------- Forwarded message ----------
Date: Sun, 21 Nov 2004 21:08:27 -0500
From: Rick Kennell <ri**@kennell.org>
To: no*****@purdue.edu
Subject: abuse of conditional operators

I was pretty bemused a few years ago when I was writing a compiler and
found out that some people expected this to work:

a ? b : c = d;

i.e. if 'a' is non-zero, assign 'd' to 'b', otherwise assign 'd' to 'c'.
Technically, there's no guarantee of support for this since X3J11
specifically claims that the conditional operator does not yield an
lvalue. GCC handles it and modern versions at least warn that this it's
deprecated behavior. TCC silently bungles it. I don't know about other
compilers.
So yesterday, I was reading LKML and came across someone who broke a
code-checking system because something was coded as:

a = b ? : c;

Specifically, the code was:

int tickadj = 500/HZ ? : 1;


Yes. GCC extensions
http://gcc.gnu.org/onlinedocs/gcc-3....s.html#Lvalues
and
http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals
Nov 14 '05 #7
Dan Noland wrote:
[...]
but it doesn't say anything about C99. Am I correct in assuming it's
just a GCCism?


If you compile using gcc with --std=c89 or --std=c99 (perhaps also -W
-Wall as well; I didn't check since I always use these), gcc will tell
you that these features are either deprecated or non-standard. Don't
use them if you want portably maintainable code.
Nov 14 '05 #8
Quoth Martin Ambuhl on or about 2004-11-24:
If you compile using gcc with --std=c89 or --std=c99 (perhaps also -W
-Wall as well; I didn't check since I always use these), gcc will tell
you that these features are either deprecated or non-standard. Don't
use them if you want portably maintainable code.


Does GCC *guarantee* to notify you? I had understood that it would only
*try* to notify you.

-trent
Nov 14 '05 #9
[Mind followups. Not sure where this would be topical; nowhere I follow,
since it's more of a pet peeve than an actual interest.]

In article <co**********@yin.interaccess.com>,
Kenny McCormack <ga*****@accessinter.combat> wrote:
In article <YI*******************@juliett.dax.net>,
=?ISO-8859-1?Q?Bj=F8rn_Augestad?= <bo*@metasystems.no> wrote:
...
Why they chose to "extend" the language like this is beyond me, since it
should be very easy to use standard C and still avoid any recomputations.


I applaud GCC and it's very successful efforts to embrace and extend
minimalist^Wstandard C.

Ever hear of this company called, er, what was that now, oh, yeah,
Microsoft? They wrote the book on e&e.


You appear to be under the misconception that the GCC folk have (or should
have) any desire to uphold standards. Stallman and company are no less
interested in locking people into their software than Gates and company
are in locking people into theirs; portability and interoperability are
for wimps who might decide that there's something better out there and
want to give up on The One True Way.
dave
(For values of `One True Way' appropriate to the subject, of course.)

--
Dave Vandervies dj******@csclub.uwaterloo.ca
80% of all questions that begin with the word 'why' can be answered
with the simple sentence 'people are stupid.'
--Shamelessly Stolen From Mike in uw.general
Nov 14 '05 #10
ga*****@yin.interaccess.com (Kenny McCormack) wrote:
In article <YI*******************@juliett.dax.net>,
=?ISO-8859-1?Q?Bj=F8rn_Augestad?= <bo*@metasystems.no> wrote:
Why they chose to "extend" the language like this is beyond me, since it
should be very easy to use standard C and still avoid any recomputations.


I applaud GCC and it's very successful efforts to embrace and extend
minimalist^Wstandard C.

Ever hear of this company called, er, what was that now, oh, yeah,
Microsoft? They wrote the book on e&e.


Micro$oft wrote E&E edition 1. The Gnu guys wrote edition 2, and added a
fat appendix on "How to play the good guy while using the commercial
bastards' methods".

Richard
Nov 14 '05 #11
<OT implementation=gcc>
On Wed, 24 Nov 2004 08:35:04 GMT, Bjørn Augestad <bo*@metasystems.no>
wrote:
Ganuck (LOL:-) allows it [as an extension]. from
http://gcc.gnu.org/onlinedocs/gcc-3....l#Conditionals

5.8 Conditionals with Omitted Operands

The middle operand in a conditional expression may be omitted. Then if
the first operand is nonzero, its value is the value of the conditional
expression. <snip> Why they chose to "extend" the language like this is beyond me, since it
should be very easy to use standard C and still avoid any recomputations.

I have always suspected, but I can't prove, it was because it makes
C's ?: behave more like the COND form in LISP. Whether that is, or
would be, a good thing is up to each viewer. I do sometimes treat C
pointers as semipredicate, with null (canonically NULL) for NIL. FWIW.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #12

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

Similar topics

8
by: Ralph Freshour | last post by:
Is it possible to inhibit the browser Back/Fwd buttons via PHP? Thanks...
4
by: TheKeith | last post by:
I just wrote the following script for something I'm working on: ---------------------------------------------------------------------------- ------------------- <html> <head> <script...
62
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
4
by: mux | last post by:
Hi I found out that the following piece of code throws an error. 1 #include "stdio.h" 2 3 int main() 4 { 5 int a,b; 6 a= 10;
10
by: nimmi_srivastav | last post by:
Below you will see an example of a nested conditional expression that this colleague of mine loves. He claims that it is more efficient that a multi-level if-else-if structure. Moreover, our...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
8
by: marcwentink | last post by:
To my surprise Visual Studio 8 - VB evaluates C_B in if C_A And C_B Then And Hence if C_B is an expression that can only be validated if C_A is true my code crashes. For example If p_EdType...
3
by: somenath | last post by:
Hi All, I have one question regarding the conditional operator. In the draft C99 standard it is mentioned that "1 The following are the sequence points described in 5.1.2.3: -- The call to a...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.