10

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?

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336

6 Answers6

20

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    Even I don't say in my question that "bool is a macro in C++" :-). I wanted to know how is it treated. Good that you have provided standard reference. – iammilind Aug 04 '13 at 11:09
  • In C++, bool is a macro defined to bool (the keyword). false is defined to false and true is defined to true. Who cares ? Me because my IDE colors bool as a macro instead of a keyword because of that... (and that's ugly) The only way I can access the real types is by using #undef – Winter Jun 27 '17 at 14:13
  • @Winter: That is not true; some code/library/header/implementation is doing that. A program that does `#define bool bool` or `#define true true` or `#define false false` actually has undefined behaviour (though I can roughly imagine a crappy stdlib implementation doing it) so it's certainly not something about "in C++" – Lightness Races in Orbit Jun 27 '17 at 14:27
  • @BoundaryImposition It is done in stdbool.h and it's indicated "Supporting in C++ is a GCC extension". I don't have anything weird with my setup, using gcc 4.9.2 on debian. – Winter Jun 27 '17 at 14:52
  • @Winter: Right, so, a C header included only as an extension by your stdlib implementation. Nothing to do with C++. Simply don't use the header and you'll be fine. – Lightness Races in Orbit Jun 27 '17 at 15:02
  • Good to know that atomic_base.h includes it which is included by atomic. – Winter Jun 27 '17 at 17:37
8

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

ouah
  • 142,963
  • 15
  • 272
  • 331
  • Only since C99. Prior to that, there was no boolean type in C whatsoever. Further, `_Bool` is _numeric_, which may not be what one expects coming from C++. Really, `_Bool` is not a "boolean type"; it is simply a numeric type large enough to store both `1` and `0`. – Lightness Races in Orbit Aug 04 '13 at 12:52
  • @LightnessRacesinOrbit I respect the C terminology which calls `_Bool` the *boolean type* in the C Standard. – ouah Aug 04 '13 at 13:06
  • 6.1.3.2 is headed "Boolean type", but only talks about conversions: `When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1.` It's a sort of faux-boolean type, by any conventional logic. – Lightness Races in Orbit Aug 04 '13 at 13:21
  • 1
    @LightnessRacesinOrbit it is the reason why it is called the boolean type. It is an integer type but conversions do not behave as other integer types. For example, `(bool) 0.5` is `1`, but `(char) 0.5` or `(int) 0.5` are `0`. The term *boolean type* is also referred in *7.18 Boolean type and values * in the Standard. – ouah Aug 04 '13 at 13:33
4

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

billz
  • 44,644
  • 9
  • 83
  • 100
1

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.

Codie CodeMonkey
  • 7,669
  • 2
  • 29
  • 45
1

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
}
  • 2
    In C, there is a type `_Bool`, and the literal constants `true` and `false` are defined in a header. And while in C, comparison operators formally result in an `int`, the language is designed so that you can program in it as if it has a true boolean type; this has been more or less recommended practice for at least 30 years. – James Kanze Aug 04 '13 at 11:25
  • It really amazed me, Thanks James, I never knew it. –  Aug 04 '13 at 11:31
  • 1
    If anyone else has doubts on boolean variables in C please visit - http://stackoverflow.com/questions/1921539/using-boolean-values-in-c This only means you can't directly make a bool variable, you need to define it using typedef keyword , Thus answer is Both because we can define it using #define true and in other language, it is also a keyword. –  Aug 04 '13 at 11:38
  • 1
    Did you read the answer. If you include ``, you can use `bool`, `true` and `false` exactly as you would in C++. Formally, they're different, but when used normally, the differences have no impact on your code. – James Kanze Aug 04 '13 at 13:16
  • Yes I did, Thanks James –  Aug 04 '13 at 13:36
1

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.

  1. bool which expands to _Bool
  2. true which expands to 1
  3. false which expands to 0
  4. __bool_true_false_are_defined which expands to 1.
ShuklaSannidhya
  • 8,572
  • 9
  • 32
  • 45