跳到內容

C 程式設計/stdbool.h

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

標頭檔案stdbool.h位於 C 程式語言的 C 標準庫中,包含四個用於布林資料型別的宏。此標頭檔案在 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: 布林型別和值 – 基本定義參考,單一 UNIX® 規範,第 7 版,來自開放組
華夏公益教科書