301

I've noticed that the Linux kernel code uses bool, but I thought that bool was a C++ type. Is bool a standard C extension (e.g., ISO C90) or a GCC extension?

Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
asussex
  • 3,051
  • 2
  • 16
  • 6
  • 3
    Section 9 of the [comp.lang.c FAQ](http://www.c-faq.com/) discusses this. – Keith Thompson Jul 11 '13 at 20:46
  • 2
    Direct link: http://www.c-faq.com/bool/index.html – Ellen Spertus May 05 '17 at 19:08
  • 1
    The Linux kernel uses `-std=gnu89` which supports `_Bool` as an extension to C90. "include/linux/types.h" has `typedef _Bool bool;`. – Ian Abbott Mar 13 '20 at 16:01
  • 1
    Also, FWIW, the Linux kernel 2.6.19 was the first version to use `typedef _Bool bool;` (commit [6e21828743247270d09a86756a0c11702500dbfb](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6e21828743247270d09a86756a0c11702500dbfb)) and it required GNU C 3.2 or later. – Ian Abbott Mar 13 '20 at 16:41

12 Answers12

419

bool exists in the current C - C99, but not in C89/90.

In C99 the native type is actually called _Bool, while bool is a standard library macro defined in stdbool.h (which expectedly resolves to _Bool). Objects of type _Bool hold either 0 or 1, while true and false are also macros from stdbool.h.

Note, BTW, that this implies that C preprocessor will interpret #if true as #if 0 unless stdbool.h is included. Meanwhile, C++ preprocessor is required to natively recognize true as a language literal.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 73
    There's a new ISO C standard, published in 2011 (after this answer was posted). ANSI, as usual, has adopted the ISO C11 standard as an ANSI standard. For historical reasons, the phrase "ANSI C" commonly (but incorrecetly) refers to the language defined by the ANSI C89 / ISO C90 standard. Since C standards are now published by ISO first, and since there have been three ISO C standards, with varying levels of adoption, it's best to refer to the year the standard was publlshed (ISO C90, ISO C99, ISO C11) to avoid any confusion. – Keith Thompson Jul 11 '13 at 20:40
  • 12
    Does this mean `_Bool` takes up 1 bit of memory? – Geremia Feb 01 '16 at 22:39
  • 33
    @Geremia: No. Why? In C each addressable object has to occupy at least 1 byte. And in real life implementations `_Bool` usually takes 1 byte of memory. However, language specification explicitly permits using `_Bool` as bit-field type, meaning that by using bit-fields you can squeeze a `_Bool` value into a single bit (inside a larger struct). – AnT stands with Russia Feb 02 '16 at 00:17
  • @AnT How could a `_Bool` value be both directly addressable (i.e. sized 1 byte) and also participate in a bit-field? An array of `_Bool` would still require all of its elements to be addressable (e.g. `_Bool* ptr = &boolArray[123]`). – Dai Apr 05 '20 at 00:05
  • 1
    @Dai Your remarks deserve to be a completely separate SO question. – Danijel Oct 19 '20 at 08:00
  • In C89 for example, how did conditionals and comparative operators work without a native bool type? If I write `if (5 < 6)`, is it all converted to ints under the hood? – Ekanshdeep Gupta Apr 04 '22 at 23:59
  • 1
    @EkanshdeepGupta Operator < returns an int - zero for false, and non-zero for true (I _think_ it is required to return 1, but I can't be bothered to look it up). The expression is in an `if` is evaluated, and then the controlled statement is executed if the expression value is non-zero (or non-NULL if the expression is of a pointer type). – Martin Bonner supports Monica Mar 21 '23 at 15:40
  • That changes with C23 https://en.cppreference.com/w/c/language/bool_constant – Aykhan Hagverdili Jun 01 '23 at 07:14
133

C99 added a builtin _Bool data type (see Wikipedia for details), and if you #include <stdbool.h>, it provides bool as a macro to _Bool.

You asked about the Linux kernel in particular. It assumes the presence of _Bool and provides a bool typedef itself in include/linux/types.h.

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • 28
    As to why, it is to allow it ot be undefined and redefined where its definition might cause a clash with legacy code. – Clifford Oct 23 '09 at 21:41
36

C99 has it in stdbool.h, but in C90 it must be defined as a typedef or enum:

typedef int bool;
#define TRUE  1
#define FALSE 0

bool f = FALSE;
if (f) { ... }

Alternatively:

typedef enum { FALSE, TRUE } boolean;

boolean b = FALSE;
if (b) { ... }
Rob
  • 5,223
  • 5
  • 41
  • 62
  • 5
    Note that the behavior of the typedef will be different from that of the C99 `bool`, and also different from that of many compilers' `bit` types. For example, `bool x=4294967296LL;` or `bool x=0.1;` would set `x` to one on C99, but would likely set most typedef versions to zero. – supercat May 18 '16 at 21:48
31

No, there is no bool in ISO C90.

Here's a list of keywords in standard C (not C99):

  • auto
  • break
  • case
  • char
  • const
  • continue
  • default
  • do
  • double
  • else
  • enum
  • extern
  • float
  • for
  • goto
  • if
  • int
  • long
  • register
  • return
  • short
  • signed
  • static
  • struct
  • switch
  • typedef
  • union
  • unsigned
  • void
  • volatile
  • while

Here's an article discussing some other differences with C as used in the kernel and the standard: http://www.ibm.com/developerworks/linux/library/l-gcc-hacks/index.html

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • 6
    For practical purposes, does it really matter so long as there is still no decent compiler support? Even gcc didn't have half of C99 features until recently, and MSVC doesn't have most of them, and probably never will... – Pavel Minaev Oct 24 '09 at 08:45
  • 5
    @Jonathan Leffler, the questioner specifically asked about ISO C90. :) In fact, usually when people refer to ANSI C they meaqn C90. I don't use or really plan to use C99 and I think many feel the same way. – BobbyShaftoe Oct 24 '09 at 16:00
  • 6
    @BobbyShaftoe: The original poster explicitly said in a comment that C90 was an example. – Keith Thompson Jul 11 '13 at 21:12
26
/* Many years ago, when the earth was still cooling, we used this: */

typedef enum
{
    false = ( 1 == 0 ),
    true = ( ! false )
} bool;

/* It has always worked for me. */
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
user2705144
  • 293
  • 3
  • 2
  • 18
    The initial values are entirely unnecessary. `typedef enum { false, true };` is just as good. If you insist on being more explicit, you can write `typedef enum { false = 0, true = 1 };`. (Or just `#include ` if your compiler supports it; it's been standard for 14 years.) – Keith Thompson Aug 21 '13 at 20:38
  • 14
    @KeithThompson Initial values may be unnecessary, but this answer chooses them in a very elegant way, not with arbitrary values, but using the languages' own semantics and letting the compiler decide. – MestreLion Feb 16 '15 at 05:21
  • 16
    @MestreLion: The language's own semantics guarantee that `typedef enum { false, true } bool;` works exactly as expected. `1 == 0` and `! false` are not elegant, they're merely obfuscated. There's no decision for the compiler to make; it must obey the semantics defined by the language. – Keith Thompson Feb 16 '15 at 05:47
  • 16
    @KeithThompson: I don't think they're obfuscated, I guess the author's intention was to choose the most "natural" values: `false` is set to whatever value the language says an inequality should be evaluated to, and `true` to its "opposite" (again, whatever that is). This way one should not care if that is {1, 0}, {-1, 0}, {0, 1}, etc, and it is guaranteed to work in comparisons, because it was *crafted* using one. – MestreLion Feb 16 '15 at 05:55
  • 4
    @MestreLion: Anyone who knows C knows the numeric values of `false` and `true`. Anyone who doesn't know C is not the expected audience for C code. And as I said, C has had a built-in Boolean type since the previous millennium. – Keith Thompson Feb 16 '15 at 05:59
  • 5
    @KeithThompson: you're right on both statements. `#include ` is the best solution for the past decade :) I was just fond of this quite "language agnostic" trick for choosing the values. – MestreLion Feb 16 '15 at 06:04
12

_Bool is a keyword in C99: it specifies a type, just like int or double.

6.5.2

2 An object declared as type _Bool is large enough to store the values 0 and 1.

pmg
  • 106,608
  • 13
  • 126
  • 198
9

C99 defines bool, true and false in stdbool.h.

nbro
  • 15,395
  • 32
  • 113
  • 196
starblue
  • 55,348
  • 14
  • 97
  • 151
5

stdbool.h defines macros true and false, but remember they are defined to be 1 and 0.

That is why sizeof(true) equals sizeof(int), which is 4 for 32 bit architectures.

Robert
  • 6,855
  • 4
  • 35
  • 43
Neha Gangwar
  • 670
  • 9
  • 14
2

stdbool.h was introduced in c99

Nick Van Brunt
  • 15,244
  • 11
  • 66
  • 92
2

C99 added a bool type whose semantics are fundamentally different from those of just about all integer types that had existed before in C, including user-defined and compiler-extension types intended for such purposes, and which some programs may have "type-def"ed to bool.

For example, given bool a = 0.1, b=2, c=255, d=256;, the C99 bool type would set all four objects to 1. If a C89 program used typedef unsigned char bool, the objects would receive 0, 2, 255, and 0, respectively. If it used char, the values might be as above, or c might be -1. If it had used a compiler-extension bit or __bit type, the results would likely be 0, 0, 1, 0 (treating bit in a way equivalent to an unsigned bit-field of size 1, or an unsigned integer type with one value bit).

supercat
  • 77,689
  • 9
  • 166
  • 211
  • 2
    I don't understand how b=2, in the C89 program using "typedef unsigned char bool", would set the object to 1? I would have expected 2... since unsigned char ch = 2; would set ch to 2... – Basya Dec 22 '22 at 11:58
2

Since C23, bool, true and false are C keywords and don't require any #includes.

bool becomes one of the fundamental builtin data types.

_Bool remains valid and is treated as "Alternative Spelling".

The header <stdbool.h> provides only the obsolescent macro __bool_true_false_are_defined which expands to the integer constant 1.

You can find the latest draft here: https://open-std.org/JTC1/SC22/WG14/www/docs/n2912.pdf

Kaznov
  • 1,035
  • 10
  • 17
-1

No such thing, probably just a macro for int

sindre j
  • 4,404
  • 5
  • 31
  • 32
  • Nice with -1's ... the question was C90, not 99 i believe – sindre j Oct 24 '09 at 15:29
  • 5
    well he says C standard *eg* C90, i assume that includes C99. – Matt Joiner Oct 29 '09 at 10:17
  • 2
    He mentiones C90 spesifically, NOT C99, so I assume that what he means. According to wikipedia the only compiler that fully supports C99 is Sun Studio from Sun Microsystems. Now, that's hardly a wide accepted standard is it ? Arguably most modern compilers DO implement parts of the C99 standard, I should probably have mentioned that to avoid stupid comments like yours! What's java or c# to do with this btw? – sindre j Oct 29 '09 at 11:28
  • 8
    *standard C extension (e.g., ISO C90)* is classifying the kind of C standards he's interested in, not specifically C90 itself. an appropriate answer to this is, **yes** a C standard such as C90, specifically the C99 standard, **does** implement a `bool` type. – Matt Joiner Oct 29 '09 at 12:03