Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.