跳轉到內容

C 程式設計/stdbool.h

來自華夏公益教科書,開放的書籍,面向開放的世界

C 程式語言的 C 標準庫中的標頭檔案 **stdbool.h** 包含用於布林資料型別的四個宏。此標頭檔案在 C99 中引入。

ISO C 標準中定義的宏是 

  • bool 展開為 _Bool
  • true 展開為 1
  • false 展開為 0
  • __bool_true_false_are_defined 展開為 1

這可以在示例中使用

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(void) {
    bool keep_going = true;  // Could also be `bool keep_going = 1;`
    while(keep_going) {
        printf("This will run as long as keep_going is true.\n");
        keep_going = false;    // Could also be `keep_going = 0;`
    }
    printf("Stopping!\n");
    return EXIT_SUCCESS;
}

這將輸出

This will run as long as keep_going is true.
Stopping!
[編輯 | 編輯原始碼]
  • stdbool.h: 布林型別和值 – 基礎定義參考,The Single UNIX® Specification,來自 The Open Group 的 Issue 7
華夏公益教科書