Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Using boolean values in C

C doesn't have any inherent boolean types. What's the most ideal approach to utilize them in C?
by

2 Answers

sandhya6gczb
There are multiple way to use boolean values in C
Option 1

#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
kshitijrana14
From best to worse:
Option 1 (C99 and newer)
#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
#Explanation


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

Login / Signup to Answer the Question.