DEV Community

BC
BC

Posted on

Bool in C Use `bool` in C program

Usually in C we use int to represent boolean values, like:

int a = 0;
Enter fullscreen mode Exit fullscreen mode

But sometimes use bool to define the boolean type, and use true & false to represent the value will make our code more clear. I just found out that you can do this by including stdbool.h file:

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

int main() {
    bool rainyDay = false;
    printf("Rainy day: %d\n", rainyDay);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Compile and run it:

Rainy day: 0
Enter fullscreen mode Exit fullscreen mode

Actually the stdbool.h just defined the bool using #define, which map the bool type to _Bool, true to 1 and false to 0. Check the full source code here.

Top comments (0)