Is bool a native C type?

CGccLinux KernelBoolean

C Problem Overview


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?

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

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

Solution 4 - C

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

Solution 5 - C

/* 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. */

Solution 6 - C

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

Solution 7 - C

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

Solution 8 - C

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.

Solution 9 - C

Solution 10 - C

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

Solution 11 - C

No such thing, probably just a macro for int

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionasussexView Question on Stackoverflow
Solution 1 - CAnTView Answer on Stackoverflow
Solution 2 - CJosh KelleyView Answer on Stackoverflow
Solution 3 - CRobView Answer on Stackoverflow
Solution 4 - CBobbyShaftoeView Answer on Stackoverflow
Solution 5 - Cuser2705144View Answer on Stackoverflow
Solution 6 - CpmgView Answer on Stackoverflow
Solution 7 - CstarblueView Answer on Stackoverflow
Solution 8 - CNeha GangwarView Answer on Stackoverflow
Solution 9 - CNick Van BruntView Answer on Stackoverflow
Solution 10 - CsupercatView Answer on Stackoverflow
Solution 11 - Csindre jView Answer on Stackoverflow