Use of bool in C

CServer Side ProgrammingProgramming

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\n");
   } else {
      printf("my_bool1 is true\n");
   }
   my_bool2 = 2;
   if(my_bool2 == F) {
      printf("my_bool2 is false\n");
   } else {
      printf("my_bool2 is true\n");
   }
}

Output

my_bool1 is false
my_bool2 is true
raja
Published on 03-May-2019 07:50:42
Advertisements