Best Place to Define TRUE/FALSE

Discussion in 'C Programming' started by David T. Ashley, Jan 14, 2007.

  1. 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?
    ------------------------------------------------------------
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Jan 14, 2007
    #1
    1. Advertising

  2. David T. Ashley

    Ian Collins Guest

    David T. Ashley wrote:
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Jan 14, 2007
    #2
    1. Advertising

  3. "David T. Ashley" <> wrote in message
    news:...
    > 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.
    news:...
    LS
     
    Lane Straatman, Jan 14, 2007
    #3
  4. David T. Ashley

    Lew Pitcher Guest

    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

    > 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
    ?
     
    Lew Pitcher, Jan 14, 2007
    #4
  5. "Ian Collins" <> wrote in message
    news:...
    > David T. Ashley wrote:
    >> 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.
    >
    > --
    > Ian Collins.


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

    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Jan 14, 2007
    #5
  6. David T. Ashley

    Random832 Guest

    2007-01-14 <>,
    David T. Ashley wrote:
    > 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.
     
    Random832, Jan 14, 2007
    #6
  7. "Lew Pitcher" <> wrote in message
    news:...
    >
    > 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.
    --
    David T. Ashley ()
    http://www.e3ft.com (Consulting Home Page)
    http://www.dtashley.com (Personal Home Page)
    http://gpl.e3ft.com (GPL Publications and Projects)
     
    David T. Ashley, Jan 14, 2007
    #7
  8. "Lew Pitcher" <> wrote in message
    news:...
    >
    > 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
     
    Lane Straatman, Jan 14, 2007
    #8
  9. David T. Ashley

    Ian Collins Guest

    David T. Ashley wrote:

    >>--
    >>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.

    --
    Ian Collins.
     
    Ian Collins, Jan 14, 2007
    #9
  10. David T. Ashley

    CBFalconer Guest

    "David T. Ashley" wrote:
    >
    > 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

    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Jan 14, 2007
    #10
  11. Ian Collins <> writes:
    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #11
  12. "Lew Pitcher" <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #12
  13. "David T. Ashley" <> writes:
    > "Lew Pitcher" <> wrote in message
    > news:...
    >> 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?


    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".

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #13
  14. "Lane Straatman" <> writes:
    > "Lew Pitcher" <> wrote in message
    > news:...
    >>
    >> 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #14
  15. Keith Thompson wrote:
    > "Lane Straatman" <> writes:
    > > "Lew Pitcher" <> wrote in message
    > > news:...
    > >>
    > >> 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?
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Jan 14, 2007
    #15
  16. David T. Ashley wrote:
    > "Ian Collins" <> wrote in message
    > news:...
    > > David T. Ashley wrote:
    > >> 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.
    > >
    > > --
    > > Ian Collins.

    >
    > 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
     
    Robert Gamble, Jan 14, 2007
    #16
  17. "CBFalconer" <> wrote in message
    news:...
    > "David T. Ashley" wrote:
    >>
    >> Where is the best place to define TRUE and 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

    This is the only header you need for the whole standard regarding operators?
    LS

    > --
    > "I conclude that there are two ways of constructing a software
    > design: One way is to make it so simple that there are obviously
    > no deficiencies and the other way is to make it so complicated
    > that there are no obvious deficiencies." -- C. A. R. Hoare
    >

    --
    Hoare's sort was an improvement on Shell's
     
    Lane Straatman, Jan 14, 2007
    #17
  18. "Harald van Dijk" <> writes:
    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 14, 2007
    #18
  19. "Keith Thompson" <> wrote in message
    news:...
    > "Harald van D?k" <> writes:
    >> 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
     
    Lane Straatman, Jan 14, 2007
    #19
  20. Lane Straatman said:
    > "Lew Pitcher" <> wrote in message
    > news:...
    >>
    >> 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


    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.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jan 14, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    667
    Chris Theis
    Jun 18, 2004
  2. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,034
    Grant Edwards
    Dec 16, 2005
  3. André
    Replies:
    3
    Views:
    1,595
  4. bdb112
    Replies:
    45
    Views:
    1,347
    jazbees
    Apr 29, 2009
  5. Shea Martin

    false or true == true .... WTF?

    Shea Martin, Apr 5, 2007, in forum: Ruby
    Replies:
    4
    Views:
    107
    Bertram Scharpf
    Apr 5, 2007
Loading...

Share This Page