| 標準 | 關鍵字 | 標頭檔 | 備註 |
|---|---|---|---|
| C89 (ANSI C) | 無 | 無 | 慣用 int 模擬 (0=false, 非0=true) |
| C99 | _Bool | <stdbool.h> | 定義 bool, true, false 宏 |
| C11/C18 | _Bool | <stdbool.h> | 同 C99,_Bool 為關鍵字 |
<stdbool.h> 內容#define bool _Bool
#define false 0
#define true 1
#if __STDC_VERSION__ >= 199901L // C99 或更新
#include <stdbool.h>
#else
#define bool int
#define true 1
#define false 0
#endif
_Bool 特殊行為_Bool a = -112;
printf("%d\n", a); // 輸出 1
// 規則:任何非零值賦給 _Bool 皆轉為 1,零為 0
#include <stdio.h>
#include <stdbool.h>
int main(void) {
_Bool a = 1;
bool b = true;
bool c = false;
if (a == true) {
printf("%d %d %d\n", a, b, c); // 1 1 0
}
return 0;
}
現代 C 專案建議:無條件包含
<stdbool.h>並使用bool/true/false。僅在極老舊編譯器 (如 Turbo C、早期 Keil) 才需手動定義。_Bool為關鍵字不可被宏重新定義。