Best Place to Define TRUE/FALSE

D

David T. Ashley

Where is the best place to define TRUE and FALSE?

Are they in any of the standard include files, ever?

Do any standards apply?

What I've traditionally done is something like:

#ifndef (TRUE)
#define TRUE (1)
#endif

(and same for FALSE).

But is there a "standard place" or a standard way of thinking about these?
 
I

Ian Collins

David said:
Where is the best place to define TRUE and FALSE?

Are they in any of the standard include files, ever?

Do any standards apply?

What I've traditionally done is something like:

#ifndef (TRUE)
#define TRUE (1)
#endif

(and same for FALSE).

But is there a "standard place" or a standard way of thinking about these?

No, it's each to his/her own, unless you use C99.
------------------------------------------------------------
Please trim this to two dashes, newsreaders look for a line "--<cr>" as
a signature separator.
 
L

Lane Straatman

David T. Ashley said:
Where is the best place to define TRUE and FALSE?

Are they in any of the standard include files, ever?

Do any standards apply?

What I've traditionally done is something like:

#ifndef (TRUE)
#define TRUE (1)
#endif

(and same for FALSE).

But is there a "standard place" or a standard way of thinking about these?
It's in stdbool.h . We just looked at it.
LS
 
L

Lew Pitcher

David said:
Where is the best place to define TRUE and FALSE?

In your own headers.
Are they in any of the standard include files, ever?

AFAICT, no. The C standard doesn't define them
Do any standards apply?

Only to the extent that they delimit what must be in the standard
headers.
What I've traditionally done is something like:

#ifndef (TRUE)
#define TRUE (1)
#endif

O....K.....

If you are going that route, then might I suggest
#ifndef FALSE
#define FALSE (0)
#define TRUE (!(FALSE))
#endif
?
 
D

David T. Ashley

Ian Collins said:
No, it's each to his/her own, unless you use C99.

Please trim this to two dashes, newsreaders look for a line "--<cr>" as
a signature separator.

OK, done. Just out of curiousity, which newsreader and platform are you
using?
 
R

Random832

2007-01-14 said:
Where is the best place to define TRUE and FALSE?

Are they in any of the standard include files, ever?

stdbool.h defines true and false in c99.
 
D

David T. Ashley

Lew Pitcher said:
If you are going that route, then might I suggest
#ifndef FALSE
#define FALSE (0)
#define TRUE (!(FALSE))
#endif

I'm not sure that this is advantageous over FALSE=0, TRUE=1. Any
advantages?

Also, I think the tests used are more significant.

This:

<begin>
if (x==FALSE)
...
else if (x==TRUE)
...
<end>

would seem to lead to trouble. In fact, maybe defining TRUE is a bad idea
.... every test should be either ==FALSE or != FALSE.
 
L

Lane Straatman

Lew Pitcher said:
In your own headers.


AFAICT, no. The C standard doesn't define them
[snip]
I think that this is an example of a statement that was true in '89 but
false in '99 . I've heard rumors about long, impassioned battles about '89
vs '99, but I've never read them. I believe that the leading proponent of
the merits of the 89 standard is Richard Heathfield, but I have not read
what he has to say categorically.

It would seem that forcing the existence of stdbool.h and with it
interoperability with common C extensions would be a good thing. LS
 
I

Ian Collins

You should also set you reader to trim other people's signatures.
OK, done. Just out of curiousity, which newsreader and platform are you
using?
Mozilla/Solaris.
 
C

CBFalconer

David T. Ashley said:
Where is the best place to define TRUE and FALSE?
Are they in any of the standard include files, ever?
Do any standards apply?

What I've traditionally done is something like:

#ifndef (TRUE)
#define TRUE (1)
#endif

(and same for FALSE).

But is there a "standard place" or a standard way of thinking
about these?

Yes, in C99. I use the following to adapt to the compiler
available. The result is that all the C99 conventions are
followed. Note lower case true/false, per C99.

/* Standard defines of operators, usable on C90 up */
#ifndef stdops_h
#define stdops_h
#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
/* The following from C99 - must define for C90 */
#include <stdbool.h> /* define bool, true, false */
#include <iso646.h> /* define not, and, or */
#else
#define false 0
#define true 1
typedef int bool;
#define not !
#define and &&
#define or ||
#define xor ^
#endif
#endif
 
K

Keith Thompson

Ian Collins said:
David T. Ashley wrote: [...]
------------------------------------------------------------
Please trim this to two dashes, newsreaders look for a line "--<cr>" as
a signature separator.

The standard signature separator is a line consisting of two dashes
and a space: "-- ". (Both of you have this right.)
 
K

Keith Thompson

Lew Pitcher said:
If you are going that route, then might I suggest
#ifndef FALSE
#define FALSE (0)
#define TRUE (!(FALSE))
#endif
?

Why bother?

#define FALSE 0
#define TRUE 1

The parentheses are not necessary, and the value of !0 will never be
anything other than 1 in any language called "C".

Section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>, covers this
quite well.
 
K

Keith Thompson

David T. Ashley said:
I'm not sure that this is advantageous over FALSE=0, TRUE=1. Any
advantages?

None at all.
Also, I think the tests used are more significant.

This:

<begin>
if (x==FALSE)
...
else if (x==TRUE)
...
<end>

would seem to lead to trouble. In fact, maybe defining TRUE is a bad idea
... every test should be either ==FALSE or != FALSE.

If you've defined FALSE and TRUE, don't compare anything for equality
to either of them.

Rather than "if (x == TRUE)", just write "if (x)".
Rather than "if (x == FALSE"), just write "if (!x)".

If you think that "if (x == TRUE)" is better than "if (x)", you should
think that "if ((x == TRUE) == TRUE)" is even better.

And "x" should have a name that makes it clear that it's a condition,
for example "done" or "more_data".
 
K

Keith Thompson

Lane Straatman said:
Lew Pitcher said:
In your own headers.


AFAICT, no. The C standard doesn't define them
[snip]
I think that this is an example of a statement that was true in '89 but
false in '99 . I've heard rumors about long, impassioned battles about '89
vs '99, but I've never read them. I believe that the leading proponent of
the merits of the 89 standard is Richard Heathfield, but I have not read
what he has to say categorically.

It would seem that forcing the existence of stdbool.h and with it
interoperability with common C extensions would be a good thing. LS

A quick summary:

Opinions vary about whether the C99 standard is a significant
improvement over the C90 standard. But regardless of that, it's a
fact that there are still very few conforming C99 implementations, so
if you want to write maximally portable code you need to stick to C90.
 
G

Guest

Keith said:
Lane Straatman said:
Lew Pitcher said:
David T. Ashley wrote:
Where is the best place to define TRUE and FALSE?

In your own headers.

Are they in any of the standard include files, ever?

AFAICT, no. The C standard doesn't define them
[snip]
I think that this is an example of a statement that was true in '89 but
false in '99 . I've heard rumors about long, impassioned battles about '89
vs '99, but I've never read them. I believe that the leading proponent of
the merits of the 89 standard is Richard Heathfield, but I have not read
what he has to say categorically.

It would seem that forcing the existence of stdbool.h and with it
interoperability with common C extensions would be a good thing. LS

A quick summary:

Opinions vary about whether the C99 standard is a significant
improvement over the C90 standard. But regardless of that, it's a
fact that there are still very few conforming C99 implementations, so
if you want to write maximally portable code you need to stick to C90.

You mean the C99-compatible subset of C90, right?
 
R

Robert Gamble

David said:
OK, done. Just out of curiousity, which newsreader and platform are you
using?

Ian has already answered this but just so you know, you can usually
obtain this information by examining the message headers, in this case
it yields:

User-Agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.7.13)
Gecko/20060510

Robert Gamble
 
L

Lane Straatman

CBFalconer said:
Yes, in C99. I use the following to adapt to the compiler
available. The result is that all the C99 conventions are
followed. Note lower case true/false, per C99.

/* Standard defines of operators, usable on C90 up */
#ifndef stdops_h
#define stdops_h
#if defined(__STDC__) && (__STDC_VERSION__ >= 199901L)
/* The following from C99 - must define for C90 */
#include <stdbool.h> /* define bool, true, false */
#include <iso646.h> /* define not, and, or */
#else
#define false 0
#define true 1
typedef int bool;
#define not !
#define and &&
#define or ||
#define xor ^
#endif
#endif
This is the only header you need for the whole standard regarding operators?
LS
 
K

Keith Thompson

Harald van Dijk said:
Keith Thompson wrote: [...]
A quick summary:

Opinions vary about whether the C99 standard is a significant
improvement over the C90 standard. But regardless of that, it's a
fact that there are still very few conforming C99 implementations, so
if you want to write maximally portable code you need to stick to C90.

You mean the C99-compatible subset of C90, right?

Yes, you're right. (Mostly that means avoiding the new keywords.)
 
L

Lane Straatman

Keith Thompson said:
Harald van D?k said:
Keith Thompson wrote: [...]
A quick summary:

Opinions vary about whether the C99 standard is a significant
improvement over the C90 standard. But regardless of that, it's a
fact that there are still very few conforming C99 implementations, so
if you want to write maximally portable code you need to stick to C90.

You mean the C99-compatible subset of C90, right?

Yes, you're right. (Mostly that means avoiding the new keywords.)
Does 'bool' count as a keyword? LS
 
R

Richard Heathfield

Lane Straatman said:
Lew Pitcher said:
In your own headers.


AFAICT, no. The C standard doesn't define them
Correct.

[snip]
I think that this is an example of a statement that was true in '89 but
false in '99 .

Not so. Neither C90 nor C99 defines TRUE. I didn't actually check for FALSE,
but I'm 99.9% certain it doesn't appear within the text of the Standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top