Saturday, October 15, 2016

C boolean type

C boolean type is not defined in standard C language before C99. the option 3) is my choice.

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!

  1. Using boolean values in C
  2. Use of bool in C
  3. Boolean Values
  4. True and False in C
  5. 3.3 Boolean Expressions
  6. Boolean type support library

No comments:

Post a Comment