neuromancer neuromancer - 1 month ago 8x
C Question

Using boolean values in C

C doesn't have any built-in boolean types. What's the best way to use them in C?


Option 1

typedef int bool;
#define true 1
#define false 0

Option 2

typedef int bool;
enum { false, true };

Option 3

typedef enum { false, true } bool;

Option 4 (C99)

#include <stdbool.h>


  • Options 1, 2 and 3 will have in practice the same identical behavior. #2 and #3 don't use #defines though, which in my opinion is better.
  • Option 4 will work only if you use C99 and it's the "standard way" to do it. Choose this if possible.

If you are undecided, go with #3!