Boolean values in C language
C - Using boolean values in C
Option – 1:
#include <stdbool.h>
The header stdbool.h in the C Standard Library for the C programming language contains four macros for a Boolean data type. The macros as defined in the ISO C standard are :
- bool which expands to _Bool
- true which expands to 1
- false which expands to 0
- __bool_true_false_are_defined which expands to 1
Example: boolean values in C using stdbool.h
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void) {
bool color = true; // Could also be `bool color = 1;`
while(color) {
printf("This will run as long as color is true.\n");
color = false;
}
printf("color is false!\n");
return 0;
}
Output:
This will run as long as color is true. color is false!
Option – 2:
typedef enum { F, T } boolean;
Example: Using typedef enum { F, T } boolean;
#include <stdio.h>
typedef enum {
F, T
}
boolean;
int main(void) {
boolean color = T;
while(color) {
printf("This will run as long as color is true.\n");
color = F;
}
printf("color is false!\n");
return 0;
}
Output:
This will run as long as color is true. color is false!
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics