In C, bool
is a macro.
There is no built-in type or keyword by the name of bool
in C, so typical implementations use the standard library to #define
true
and false
to 1
and 0
respectively. Rules such as those for the if
statement are defined in terms of "zero" and "non-zero" expressions, and therefore rely on the expanded macro definitions of true
and false
:
[C99: 6.8.4.1/2]:
In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.
For convenience, C99 added the built-in intermediate type _Bool
, and implementations of this language typically #define
bool
to _Bool
. This type is defined thus:
[C99: 6.2.5/2]:
An object declared as type _Bool
is large enough to store the values 0 and 1.
This allows for greater compatibility with C++ programs, which may include declarations of functions using the bool
type; really, though, #define _Bool int
would probably have sufficed.
In C++, bool
is both a built-in type and a keyword.
The link you provided doesn't say that bool
is a macro in C++. It says:
The purpose in C of this header is to add a bool type and the true and false values as macro definitions.
In C++, which supports those directly, the header simply contains a macro that can be used to check if the type is supported.
And this is correct.
Semantically (that is, in terms of "meaning" of your code), [C++11: 3.9.1/2]
defines bool
as an integral type in C++.
Lexically (that is, in terms of "appearance" in your code), [C++11: 2.12/1]
lists it as a keyword. In fact, all tokens that are part of the names of integral types are also keywords, including (but not limited to):
int
unsigned
long
bool
short
signed
It is, however, never a macro in C++. Instead, you get a macro __bool_true_false_are_defined
which you could use in multi-language code to switch treatment of bool
depending on whether you're working in C or C++; I'm not sure I can think of a useful example, mind you.