stdbool.h and backward compatibility

T

Tristan Miller

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
 
G

Goran Larsson

Tristan Miller said:
# define bool _Bool
# define false 0
# define true 1
# define __bool_true_false_are_defined 1
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?

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.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top