Use of bool in C


In C there is no predefined datatype as bool. We can create bool using enum. One enum will be created as bool, then put the false, and true as the element of the enum. The false will be at the first position, so it will hold 0, and true will be at second position, so it will get value 1. Now we can use this as datatype.

Example

#include<stdio.h>
typedef enum {
   F, T
}
boolean;
main() {
   boolean my_bool1, my_bool2;
   my_bool1 = F;
   if(my_bool1 == F) {
      printf("my_bool1 is false
");    } else {       printf("my_bool1 is true
");    }    my_bool2 = 2;    if(my_bool2 == F) {       printf("my_bool2 is false
");    } else {       printf("my_bool2 is true
");    } }

Output

my_bool1 is false
my_bool2 is true

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements