Sunday, September 20, 2020

BOOLEAN DATA TYPE IN C AND C++

A boolean is a data type in the C Standard Library which can store true or false. Every non-zero value corresponds to true while 0 corresponds to false.

The boolean works as it does in C++. However, if you don’t include the header file​ stdbool.h, the program will not compile.

svg viewer

Another option is to define our own data type using typedef, which takes the values true and false:

 typedef enum {false, true} bool;

However, it is safer to rely on the standard boolean in stdbool.h

 

Examples


1. Using the header file stdbool.h

 

include <stdio.h>
#include <stdbool.h>

int main() {
  bool x = false;
  if(x){
    printf("x is true.");
  }
  else{
    printf("x is false.");
  }
}
       
 
 USING TYPEDEF
 
 
 

#include <stdio.h>

typedef enum {false, true} bool;
int main() {
  bool x = false;
  if(x){
    printf("x is true.");
  }
  else{
    printf("x is false.");
  }
}
 
 

#include <stdio.h> #include <stdbool.h> int main() { bool x = false; if(x){ printf("x is true."); } else{ printf("x is false."); } }

No comments:

Post a Comment

CSS QUIZ 9 ANSWERS

  Computer system security quiz 9 Solution | Css quiz week 9 Answer with Reason | css quiz aktu Computer System Security Quiz week  9 Soluti...