Using true and false in C
Solution 1:
Just include <stdbool.h>
if your system provides it. That defines a number of macros, including bool
, false
, and true
(defined to _Bool
, 0, and 1 respectively). See section 7.16 of C99 for more details.
Solution 2:
Just use 0 or 1 directly in the code.
For C programmers, this is as intuitive as true or false.
Solution 3:
I usually do a:
typedef enum {FALSE = 0, TRUE} boolean;
Solution 4:
With the stdbool.h defined bool type, problems arise when you need to move code from a newer compiler that supports the bool type to an older compiler. This could happen in an embedded programming environment when you move to a new architecture with a C compiler based on an older version of the spec.
In summation, I would stick with the macros when portability matters. Otherwise, do what others recommend and use the bulit in type.