Using the Intrinsic built-in Type Bool
suggest changeAdded in the C standard version C99, _Bool is also a native C data type. It is capable of holding the values 0 (for false) and 1 (for true).
#include <stdio.h>
int main(void) {
_Bool x = 1;
_Bool y = 0;
if(x) /* Equivalent to if (x != 0). A _Bool holds only 0 or 1. */
{
puts("This will print!");
}
if (!y) /* Equivalent to if (y == 0) */
{
puts("This will also print!");
}
}
_Bool is an integer type with special conversion rules. The result matches the test performed by an if condition. In the following
_Bool z = X;
- If
Xhas an arithmetic type (is any kind of number),zbecomes0ifX == 0. Otherwisezbecomes1. - If
Xhas a pointer type,zbecomes0ifXis a null pointer and1otherwise.
For the spellings bool, false, and true, include <stdbool.h>. In C23 those three are keywords, and _Bool remains available.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents