stdbool.h and backward compatibility

Discussion in 'C Programming' started by Tristan Miller, Dec 11, 2003.

  1. Greetings.

    I have a program written in C99 which uses the bool, true, and false
    definitions from <stdbool.h>. Without rewriting the code, I'd like to
    make this program compatible with C89. I'm using GNU Autoconf, whose
    configure script will automatically do a #define HAVE_STDBOOL_H 1 if
    the user's compiler has a C99-conformant <stdbool.h> and a #define
    HAVE__BOOL 1 if the user's compiler provides a type _Bool. The
    Autoconf manual seems to suggest that the following code be used in
    one's program instead of #include <stdbool.h>:

    #if HAVE_STDBOOL_H
    # include <stdbool.h>
    #else
    # if ! HAVE__BOOL
    # ifdef __cplusplus
    typedef bool _Bool;
    # else
    typedef unsigned char _Bool;
    # endif
    # endif
    # define bool _Bool
    # define false 0
    # define true 1
    # define __bool_true_false_are_defined 1
    #endif

    Is this good advice? Specifically, how wise is it to be #defining a
    preprocessor macro with two leading underscores? I thought such macros
    were reserved for the implementation. What's the purpose of this
    macro, anyway? It's not referred to anywhere else in the Autoconf
    documentation, so am I correct in thinking it's required by the quirk
    of some specific implementation that ought to be compensated for?

    Regards,
    Tristan
     
    Tristan Miller, Dec 11, 2003
    #1
    1. Advertisements

  2. The purpose of the __bool_true_false_are_defined macro is to
    mimic the ISO/IEC 9899:1999 standard.

    From ISO/IEC 9899:1999, 7.16 Boolean type and values <stdbool.h>

    | 1 The header <stdbool.h> defines four macros.
    | 2 The macro
    | bool
    | expands to _Bool.
    | 3 The remaining three macros are suitable for use
    | in #if preprocessing directives. They are
    | true
    | which expands to the integer constant 1,
    | false
    | which expands to the integer constant 0, and
    | __bool_true_false_are_defined
    | which expands to the integer constant 1.
     
    Goran Larsson, Dec 13, 2003
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.