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__ */
clang's stdbool.h
#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;
?
(gcc stdint.h and stdbool.h)
stdbool.h
defines bool
as a macro because the C standard (section 7.18) says bool
shall be defined as a macro, and stdint.h
defines intN_t
etc as typedefs because the C standard (section 7.20) says intN_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:
Notwithstanding the provisions of 7.1.3, a program may undefine and perhaps then redefine the macros bool, true, and false.
If bool
were a typedef and true
and false
were, I don't know, 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 making bool
, true
, and false
keywords as they are in C++: they wanted to preserve compatibility with old programs that defined bool
, true
, and false
themselves, even if those programs use third-party headers that include stdbool.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:
1) The header
<stdbool.h>
defines four macros.2) The macro
bool
expands to
_Bool
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With