Is bool a native C type?

Viewed 377161

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

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.

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

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

Related