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?
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
We can define bool using typedef:
typedef int bool;
#define FALSE 0
#define TRUE (-1)
The C99 version of C provides the <stdbool.h>
header that defines a built-in boolean type
bool
exists in the current ANSI 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
.
C99 has it in stdbool.h, but in C90 it must be defined as a typedef or enum.
typedef int bool;
#define FALSE 0
#define TRUE (-1)
...
bool f = FALSE;
...
if (f) { ... }
OR
typedef enum { FALSE, TRUE } boolean;
...
boolean b = FALSE;
...
if (b) { ... }
Wikipedia is your friend. :)
_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.
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.