使用 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一個很好的拼寫。當數字或指標轉換為數字或指標時,它有特殊的規則。