C 中的布林值

介紹如何在C中使用布林值 C 最初並不原生支援布林值。 C99 是在 1999/2000 年發布的版本,引入了布林型別。 不過,要使用它,你需要匯入一個標頭檔,所以我不確定我們是否可以技術上稱之為 “原生”。無論如何,我們確實有一個 bool 型別。 你可以像這樣使用它: #include <stdio.h> #include <stdbool.h> int main(void) { bool isDone = true; if (isDone) { printf("完成\n"); } isDone = false; if (!isDone) { printf("未完成\n"); } } 如果你在編寫 Arduino 程式,你可以使用 bool 而不需要包含 stdbool,因為 bool 是一個有效且內建的 C++ 資料型別,而Arduino 語言則是基於 C++。 在純 C 中,記得要 #include <stdbool.h>,否則你在宣告和使用 bool 變數時會遇到一堆錯誤訊息: ➜ ~ gcc hello.c -o hello; ./hello hello.c:4:3: error: use of undeclared identifier 'bool' bool isDone = true; ^ hello....