In C, the bool type is used to represent Boolean values, which can be either true or false. Although C originally didn’t have a native Boolean type, it was introduced with the C99 standard.
Usage:
To use bool, you need to include the <stdbool.h> header file:
c
#include <stdbool.h>
Key Points:
– bool is an alias for _Bool, which is a built-in data type in C99.
– The values true and false are macros defined as 1 and 0, respectively.
Example:
c
#include <stdio.h>
#include <stdbool.h>
int main() {
bool isHappy = true;
if (isHappy) {
printf(“I am happy!\n”);
} else {
printf(“I am not happy.\n”);
}
return 0;
}
Output:
I am happy!
The bool type makes code more readable and explicitly conveys the intent of Boolean operations