I referred this question, in which some of the answers suggest that bool
is an integral type (IDEs also treat it as a keyword).
However, none of the answers suggest the information provided in cplusplus, which says that bool
is a macro which is added through <cstdbool>
(In that case, the compilers might be implicitly adding this header while compiling to allow bool
). Here is the g++ version of <stdbool.h>
.
So what exactly the bool
is? A an integral type keyword or a macro?
In C++, it's called Boolean literal, it's build in type.
§2.14.7
Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.
§2.14.6
Boolean literals
The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.
§ 3.9.1.6
In C++
bool
is a built-in data type. In C it is not, so if you're usingbool
in C it has been implemented as a typedef or with#define
, andtrue
andfalse
must have been implemented with#define
or perhaps are constants.In C
_Bool
is a type andbool
,true
andfalse
are macros defined instdbool.h
ISO C11 standard states (in section 6.2.5 Types )
stdbool.h
defines 4 macros.bool
which expands to_Bool
true
which expands to1
false
which expands to0
__bool_true_false_are_defined
which expands to1
.In C there is no concept like Boolean variables, Yes Higher level languages like Java, C# and other provides us the facility to declare a Boolean variable, that we use for flagging purposes to set it either true or false.
But you can implement this using integrals like we did in C
In C
bool
is a macro fromstdbool.h
that expands to_Bool
which is the C boolean type.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
andfalse
to1
and0
respectively. Rules such as those for theif
statement are defined in terms of "zero" and "non-zero" expressions, and therefore rely on the expanded macro definitions oftrue
andfalse
: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: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:And this is correct.
Semantically (that is, in terms of "meaning" of your code),
[C++11: 3.9.1/2]
definesbool
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 ofbool
depending on whether you're working in C or C++; I'm not sure I can think of a useful example, mind you.