stdbool.h

stdbool.h

stdbool.h是C標準函式館中一個常用的頭檔案。它定義了一個布爾類型,於C99中加入。

簡介,實現,示例,VSC支持情況,

簡介

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

實現

在不支持C99的編譯器中(如Visual C++ 6.0),可通過以下方式實現布爾類型。
#define TRUE 1#define FALSE 0typedef 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類型的長度。

示例

#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));}

VSC支持情況

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

  

相關詞條

熱門詞條

聯絡我們