views:

1315

answers:

10

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?

+9  A: 

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

BobbyShaftoe
C99 is the current standard. You're referring to a standard that is twenty years old and has been superseded (officially) for ten years.
Jonathan Leffler
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
@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
A: 

No such thing, probably just a macro for int

sindre j
C99 added this.
Novelocrat
Nice with -1's ... the question was C90, not 99 i believe
sindre j
EHHH</buzzer>. go back to java or c#
Matt Joiner
EHHHH yourself, I dare you to find a native bool type in C90
sindre j
well he says C standard *eg* C90, i assume that includes C99.
Matt Joiner
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
*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
A: 

It's not native to C. Just use int. It is supported by any c compiler.

eyalm
C99 was published a decade ago.
Novelocrat
The question was C90, NOT C99
sindre j
+1  A: 

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

Amro
+1  A: 

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

starblue
+31  A: 

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.

AndreyT
although ISO C99 was adopted by ANSI in 2000, you're the first person I've ever heard referring to it as ANSI C
Christoph
+1  A: 

stdbool.h was introduced in c99

Nick
+5  A: 

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

Rob
-1 for `#define TRUE (-1)` - had enough horrors with that resulting from COM choosing the same approach.
Georg Fritzsche
Irrelevant. The question is not about implementation, and my response correctly illustrates the concept. Nowhere does it say this is the best approach.
Rob
+4  A: 

_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
+5  A: 

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
Clearest and most accurate answer so far.
Clifford
Not entirely :) `bool` in C99 is a *macro* from `stdbool.h`, not a typedef. I don't really know why.
AndreyT
Thanks for the correction.
Josh Kelley
As to why, it is to allow it ot be undefined and redefined where its definition might cause a clash with legacy code.
Clifford