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, 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.
In C bool
is a macro from stdbool.h
that expands to _Bool
which is the C boolean type.
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
boolean-literal:
false
true
The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.
§ 3.9.1.6
Values of type bool are either true or false. [ Note: There are no signed, unsigned, short, or long
bool types or values. — end note ] Values of type bool participate in integral promotions (4.5).
In C++ bool
is a built-in data type. In C it is not, so if you're using bool
in C it has been implemented as a typedef or with #define
, and true
and false
must have been implemented with #define
or perhaps are constants.
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
if(1)
{
// Because C treats 1 and any other integer as true
}
if(0)
{
// This time our if condition will result in false
}
In C _Bool
is a type and bool
, true
and false
are macros defined in stdbool.h
ISO C11 standard states (in section 6.2.5 Types )
An object declared as type _Bool is large enough to store the values 0 and 1.
stdbool.h
defines 4 macros.
bool
which expands to _Bool
true
which expands to 1
false
which expands to 0
__bool_true_false_are_defined
which expands to 1
.