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?
12 Answers
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.
8 Comments
_Bool takes up 1 bit of memory?_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)._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]).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.
1 Comment
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) { ... } 1 Comment
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.No, there is no bool in ISO C90.
Here's a list of keywords in standard C (not C99):
autobreakcasecharconstcontinuedefaultdodoubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedstaticstructswitchtypedefunionunsignedvoidvolatilewhile
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
3 Comments
/* 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. */ 6 Comments
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 <stdbool.h> if your compiler supports it; it's been standard for 14 years.)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.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.#include <stdbool.h> is the best solution for the past decade :) I was just fond of this quite "language agnostic" trick for choosing the values.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
Comments
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).
1 Comment
No such thing, probably just a macro for int
4 Comments
bool type.
-std=gnu89which supports_Boolas an extension to C90. "include/linux/types.h" hastypedef _Bool bool;.typedef _Bool bool;(commit 6e21828743247270d09a86756a0c11702500dbfb) and it required GNU C 3.2 or later.