Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is bool in C/C++? A keyword or a macro?

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?

like image 483
iammilind Avatar asked Aug 04 '13 11:08

iammilind


3 Answers

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.

like image 73
Lightness Races in Orbit Avatar answered Oct 24 '22 18:10

Lightness Races in Orbit


In C bool is a macro from stdbool.h that expands to _Bool which is the C boolean type.

like image 20
ouah Avatar answered Oct 24 '22 18:10

ouah


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).

like image 35
billz Avatar answered Oct 24 '22 19:10

billz