使用 stdbool.h

Version >= C99

使用系统头文件 stdbool.h 允许你使用 bool 作为布尔数据类型。true 评估为 1false 评估为 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!");
    }
}

bool 只是数据类型 _Bool一个很好的拼写。当数字或指针转换为数字或指针时,它有特殊的规则。