Why do major compilers use typedef for stdint.h but use #define for stdbool.h?
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
.
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.