I just noticed that gcc and clang both appear to use typedefs for stdint.h but #define for stdbool.h.
example: clang's stdint.h
#ifdef __INT8_TYPE__
#ifndef __int8_t_defined /* glibc sys/types.h also defines int8_t*/
typedef __INT8_TYPE__ int8_t;
#endif /* __int8_t_defined */
typedef __UINT8_TYPE__ uint8_t;
# define __int_least8_t int8_t
# define __uint_least8_t uint8_t
#endif /* __INT8_TYPE__ */
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension. */
#define _Bool bool
#define bool bool
#define false false
#define true true
#endif
Why isn't it typedef _Bool bool;
?
stdbool.h
definesbool
as a macro because the C standard (section 7.18) saysbool
shall be defined as a macro, andstdint.h
definesintN_t
etc as typedefs because the C standard (section 7.20) saysintN_t
etc shall be defined as typedefs.Okay, why does the C standard say these things? I cannot tell you for sure, but a clue is in section 7.18 paragraph 4:
If
bool
were a typedef andtrue
andfalse
were, I dunno,enum
constants, they couldn't have allowed you to do that, as there is no way to undo those kinds of declarations.Okay, why does the C committee want to allow you to do that? This is even more speculative, but probably for the same reason they added
stdbool.h
and_Bool
instead of makingbool
,true
, andfalse
keywords as they are in C++: they wanted to preserve compatibility with old programs that definedbool
,true
, andfalse
themselves, even if those programs use third-party headers that includestdbool.h
...No such backward compatibility concerns apply to the types defined by
stdint.h
; some systems provided (some) of them as extensions, but they were always typedefs.I think this is just part of the standard.
If you go to page 253, under "7.16 Boolean type and values ", it clearly says: