C doesn\'t have any built-in boolean types. What\'s the best way to use them in C?
If you are using C99 then you can use the _Bool
type. No #include
s are necessary. You do need to treat it like an integer, though, where 1
is true
and 0
is false
.
You can then define TRUE
and FALSE
.
_Bool this_is_a_Boolean_var = 1;
//or using it with true and false
#define TRUE 1
#define FALSE 0
_Bool var = TRUE;
From best to worse:
Option 1 (C99)
#include <stdbool.h>
Option 2
typedef enum { false, true } bool;
Option 3
typedef int bool;
enum { false, true };
Option 4
typedef int bool;
#define true 1
#define false 0
If you are undecided, go with #1!
You could use _Bool, but the return value must be an integer (1 for true, 0 for false). However, It's recommended to include and use bool as in C++, as said in this reply from daniweb forum, as well as this answer, from this other stackoverflow question:
_Bool: C99's boolean type. Using _Bool directly is only recommended if you're maintaining legacy code that already defines macros for bool, true, or false. Otherwise, those macros are standardized in the header. Include that header and you can use bool just like you would in C++.
Just a complement to other answers and some clarification, if you are allowed to use C99.
+-------+----------------+-------------------------+--------------------+
| Name | Characteristic | Dependence in stdbool.h | Value |
+-------+----------------+-------------------------+--------------------+
| _Bool | Native type | Don't need header | |
+-------+----------------+-------------------------+--------------------+
| bool | Macro | Yes | Translate to _Bool |
+-------+----------------+-------------------------+--------------------+
| true | Macro | Yes | Translate to 1 |
+-------+----------------+-------------------------+--------------------+
| false | Macro | Yes | Translate to 0 |
+-------+----------------+-------------------------+--------------------+
Some of my preferences:
_Bool
or bool
? Both are fine, but bool
looks better than the keyword _Bool
.bool
and _Bool
are: false
or true
. Assigning 0
or 1
instead of false
or true
is valid, but is harder to read and understand the logic flow.Some info from the standard:
_Bool
is NOT unsigned int
, but is part of the group unsigned integer types. It is large enough to hold the values 0
or 1
.bool
true
and false
but sure is not a good idea. This ability is considered obsolescent and will be removed in future._Bool
or bool
, if the scalar value is equal to 0
or compares to 0
it will be 0
, otherwise the result is 1
: _Bool x = 9;
9
is converted to 1
when assigned to x
._Bool
is 1 byte (8 bits), usually the programmer is tempted to try to use the other bits, but is not recommended, because the only guaranteed that is given is that only one bit is use to store data, not like type char
that have 8 bits available.This is what I use:
enum {false, true};
typedef _Bool bool;
_Bool
is a built in type in C. It's intended for boolean values.
Anything nonzero is evaluated to true in boolean operations, so you could just
#define TRUE 1
#define FALSE 0
and use the constants.