複製鏈接
請複製以下鏈接發送給好友

stdbool.h

鎖定
stdbool.h是C標準函數庫中一個常用的頭文件。它定義了一個布爾類型,於C99中加入。
外文名
stdbool.h
所屬學科
數學

stdbool.h簡介

C 編程語言從 C99 開始支持以內建類型 _Bool 進行的布爾運算。包含頭文件 <stdbool.h> 時,布爾類型亦可用作 bool 。
宏名稱
展開
bool
_Bool
true
整數常量1
false
整數常量0
__bool_true_false_are_defined
整數常量1

stdbool.h實現

在不支持C99的編譯器中(如Visual C++ 6.0),可通過以下方式實現布爾類型。
#define TRUE 1
#define FALSE 0
typedef int bool;
在支持C99的編譯器中可以使用#include<stdbool.h>,在VSC中該頭文件內容如下:
//

// stdbool.h

//

//      Copyright (c) Microsoft Corporation. All rights reserved.

//

// The C Standard Library <stdbool.h> header.//#ifndef _STDBOOL#define _STDBOOL

#define __bool_true_false_are_defined 1

#ifndef __cplusplus

#define bool _Bool#define false 0#define true 1

#endif /* __cplusplus */

#endif /* _STDBOOL */

/*

 * Copyright (c) 1992-2010 by P.J. Plauger.  ALL RIGHTS RESERVED.

 * Consult your license regarding permissions and restrictions.

V5.30:0009 */
兩者的差別在於使用sizeof(bool)時,前者獲取的是int類型的長度。

stdbool.h示例

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

int main(void)
{
 bool a = true, b = false;
 printf("a = %d,  b = %d\n", a, b);
 printf("sizeof(_Bool) = %d\n", sizeof(_Bool));
}

stdbool.hVSC支持情況

從歷史上看,Microsoft在其Visual C ++工具中實現新的C語言功能的速度很慢,其主要側重於支持C ++標準的發展。但是,隨着Visual C ++ 2013的引入,Microsoft實現了C99的有限子集,它在Visual C ++ 2015中進行了擴展。 [1] 
參考資料