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

Need for the ternary operator

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?
Nov 14 '05 #1
6 2742
On 15 May 2004 16:14:56 -0700, gl*******@yahoo.com (glongword) wrote:
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?


Here's how the assert macro is defined in the VC6 library (the first and
only one I looked at):

#define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0)
)
I don't see any ternary operators there. So I guess assert can be
implemented without one. I don't understand your final question, but if
you're asking whether the ternary operator is in the language specifically
to support assert macros, then I seriously doubt it.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
Leor Zolman wrote:
On 15 May 2004 16:14:56 -0700, gl*******@yahoo.com (glongword) wrote:

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?

Here's how the assert macro is defined in the VC6 library (the first and
only one I looked at):

VC6 isn't the only library. also note that it makes reference to _assert()
#define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0)
)
here is the contents of Cygwin's assert.h:

/*
assert.h
*/

#ifdef __cplusplus
extern "C" {
#endif

#include "_ansi.h"

#undef assert

#ifdef NDEBUG /* required by ANSI standard */
#define assert(p) ((void)0)
#else

#ifdef __STDC__
#define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
#else /* PCC */
#define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, "e"))
#endif

#endif /* NDEBUG */

void _EXFUN(__assert,(const char *, int, const char *));

#ifdef __cplusplus
}
#endif

it very much uses the ternary operator.

the copy on my Gentoo box is similar, but more extensive. it also uses
the ternary operator.

I don't see any ternary operators there. So I guess assert can be
implemented without one.
you might want to check the definition of _assert() before you make that
statement.
I don't understand your final question, but if
you're asking whether the ternary operator is in the language specifically
to support assert macros, then I seriously doubt it.
-leor

--
-- Charles Banas
Nov 14 '05 #3
Charles Banas <uc*@ftc.gov> wrote in message news:<10*************@news.supernews.com>...
I don't see any ternary operators there. So I guess assert can be
implemented without one.
>

you might want to check the definition of _assert() before you make that
statement.
> I don't understand your final question, but if
you're asking whether the ternary operator is in the language specifically
to support assert macros, then I seriously doubt it.
-leor


Right, I think that assert needs the ternary operator because it
needs to make a decision and it cannot use the if statement. The
implementation of assert itself might not be done in C, though. So, we
might not need the ternary operator after all. I wish to clarify if
the ternary operator is just a convenience or if it is needed to
support the standard.
Nov 14 '05 #4
On Sat, 15 May 2004 21:02:30 -0600, Charles Banas <uc*@ftc.gov> wrote:
Leor Zolman wrote:
On 15 May 2004 16:14:56 -0700, gl*******@yahoo.com (glongword) wrote:

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?

Here's how the assert macro is defined in the VC6 library (the first and
only one I looked at):

VC6 isn't the only library. also note that it makes reference to _assert()


I never said it was "the only library", and I'm not sure why you feel the
need to clarify that for me.

I /did/ notice that the assert macro makes reference to _assert(). I don't
have the source to that, or actually I don't know whether or not I have
the source, and I don't particularly care. The fact it is a function call
means that I'm fairly confident that, even if it /did/ use the ternary
operator somewhere within it, it could trivially be re-written not to. And,
of course, as the OP has pointed out, _assert need not necessarily even be
written in C, but I didn't feel the need to get that desperate in looking
for a way out of requiring the existence of the ternary operator.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #5
On 15 May 2004 16:14:56 -0700, gl*******@yahoo.com (glongword) wrote:
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?


The ternary operator was in C from the beginning (or near enough to it),
so the assert macro has nothing to do with its presence. If anything, it
wouldn't surprise me to find the existence of the ternary operator
influenced the design of the assert macro.

--
Eric Amick
Columbia, MD
Nov 14 '05 #6
In <7e**************************@posting.google.com > gl*******@yahoo.com (glongword) writes:
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?
Although not idiomatic in C (but relatively popular in Perl and Unix shell
scripting), the && and || operators can replace if statements by ordinary
expression statements, due to their shortcircuiting effect.

flag && i++; is the equivalent of if (flag) i++;
flag || i++; if the equivalent of if (!flag) i++;
Are these decisions related in anyway?


The conditional operator (there is only one in C) predates the assert()
macro by a long shot.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7

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

Similar topics

7
by: Phil Powell | last post by:
$sortBy = ($_POST) ? $_POST : ($_GET) ? $_GET : 1; This script should set $sortBy to the $_POST or the $_GET or the default of 1 in one line. I do not want to have to do this:
6
by: praba kar | last post by:
Dear All, I am new to Python. I want to know how to work with ternary operator in Python. I cannot find any ternary operator in Python. So Kindly clear my doubt regarding this ...
4
by: Bob Gregory | last post by:
Hi all, I don't actually have a mac with which to test this, but I'm informed by a colleague that one of my scripts has failed in IE on the Mac; endless twiddling seems to point to the ternary...
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...
15
by: Arthur Dent | last post by:
Hi all, im just curious if anyone knows..... With .NET 2, VB didnt happen to get a true ternary operator, did it? Stuck away in a corner somewhere? By ternary, i mean something like C's a?b:c...
4
by: raiderdav | last post by:
I understand how the ternary operator (question mark - ?) works in an if/else setting, but what does it mean when used as a type? For instance, I'm trying to add a get/set in some existing code,...
8
by: Bob Hoeppner | last post by:
In the following code //add to bool or double Dictionary this.m_unit.Add((unittype == "b")? unitnum:(double)unitnum); The bool dictionary uses an integer index, the double uses a double...
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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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?
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
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.