gcc's <stdbool.h> is broken

Bruno Haible haible@ilog.fr
Mon Feb 5 10:30:00 GMT 2001


Hi,

ISO C 99 section 7.16 specifies that the macros 'false' and 'true'
shall be usable in preprocessor expressions. But gcc 2.95.2/2.95.3
gets this wrong.

========================== foo.c =========================
#include <stdbool.h>
int main ()
{
  int x = 0
#if false
          + 1
#endif
#if true
          + 2
#endif
          ;
  return (x != 2);
}
===========================================================
$ gcc -v
Reading specs from /packages/gnu/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/specs
gcc version 2.95.3 20010101 (prerelease)
$ gcc foo.c
$ ./a.out
$ echo $?
1

Here is a replacement for <stdbool.h> that gets this right.

===========================================================
#ifndef _STDBOOL_H
#define _STDBOOL_H

/* ISO C 99 <stdbool.h> for platforms that lack it.  */

/* 7.16. Boolean type and values */

/* For the sake of symbolic names in gdb, define _Bool as an enum type.  */
typedef enum { false = 0, true = 1 } _Bool;
#define bool _Bool

/* The other macros must be usable in preprocessor directives.  */
#define false 0
#define true 1
#define __bool_true_false_are_defined 1

#endif /* _STDBOOL_H */
===========================================================


More information about the Gcc-bugs mailing list