Unlike the int, char or float types, the ANSI C standard doesn’t have a built-in or primary Boolean type. A Boolean or bool data generally refers to the one that can hold one of the two binary values: true or false (or yes/no, on/off, etc.). Even if the bool type is not available in C, you can implement the behaviour of Booleans with the help of an enum type.
The new versions of C compilers, complying with the C99 standard or later, support the bool type, which has been defined in the header file stdbool.h.
Using enum to Implement Boolean Type in C
The enum type assigns user-defined identifiers to integral constants. We can define an enumerated type with true and false as the identifiers with the values 1 and 0.
Example
1 or any other number that is not 0 represents true, whereas 0 represents false.
#include <stdio.h> int main (){ enum bool {false, true}; enum bool x = true; enum bool y = false; printf ("%dn", x); printf ("%dn", y); }
Output
Run the code and check its output −
1 0
typedef enum as BOOL
To make it more concise, we can use the typedef keyword to call enum bool by the name BOOL.
Example 1
Take a look at the following example −
#include <stdio.h> int main(){ typedef enum {false, true} BOOL; BOOL x = true; BOOL y = false; printf ("%dn", x); printf ("%dn", y); }
Here too, you will get the same output −
Output
1 0
Example 2
We can even use the enumerated constants in the decision-making or loop statements −
#include <stdio.h> int main(){ typedef enum {false, true} BOOL; int i = 0; while(true){ i++; printf("%dn", i); if(i >= 5) break; } return 0; }
Output
When you run this code, it will produce the following output −
1 2 3 4 5
Boolean Values with #define
The #define preprocessor directive is used to define constants. We can use this to define the Boolean constants, FALSE as 0 and TRUE as 1.
Example
Take a look at the following example −
#include <stdio.h> #define FALSE 0 #define TRUE 1 int main(){ printf("False: %d n True: %d", FALSE, TRUE); return 0; }
Output
Run the code and check its output −
False: 0 True: 1
Boolean Type in stdbool.h
The C99 standard of C has introduced the stdbool.h header file. It contains the definition of bool type, which actually is a typedef alias for _bool type. It also defines the macros true which expands to 1, and false which expands to 0.
Example 1
We can use the bool type as follows −
#include <stdio.h> #include <stdbool.h> int main(){ bool a = true; bool b = false; printf("True: %dn", a); printf("False: %d", b); return 0; }
Output
On executing this code, you will get the following output −
True: 1 False: 0
Example 2
We can use bool type variables in logical expressions too, as shown in the following example −
#include <stdio.h> #include <stdbool.h> int main(){ bool x; x = 10 > 5; if(x) printf("x is Truen"); else printf("x is Falsen"); bool y; int marks = 40; y = marks > 50; if(y) printf("Result: Passn"); else printf("Result: Failn"); }
Output
Run the code and check its output −
x is True Result: Fail
Example 3
Let us implement a while loop with the help of a bool variable −
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main(void){ bool loop = true; int i = 0; while(loop){ i++; printf("i: %d n", i); if (i >= 5) loop = false; } printf("Loop stopped!n"); return EXIT_SUCCESS; }
Output
When you run this code, it will produce the following output −
i: 1 i: 2 i: 3 i: 4 i: 5 Loop stopped!