Using stdbool.h
suggest changeUsing the system header file stdbool.h allows you to use bool as a Boolean data type. true evaluates to 1 and false evaluates to 0.
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool x = true; /* equivalent to bool x = 1; */
bool y = false; /* equivalent to bool y = 0; */
if (x) /* Functionally equivalent to if (x != 0) or if (x != false) */
{
puts("This will print!");
}
if (!y) /* Functionally equivalent to if (y == 0) or if (y == false) */
{
puts("This will also print!");
}
}
In C99 through C17, bool is a macro for _Bool. C23 adds bool, true, and false as keywords; the header remains available. Conversions to bool still use the _Bool rules: zero and null become false, and every other scalar value becomes true.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents