【原創】bool、BOOL 和 _Bool 辨析


      最近在搞跨平臺編譯的時候又遇到了 C99 標準支持的問題,主要體如今布爾類型問題上面。因而乎決定把這個問題完全搞搞清楚,遂成此文。

bool、BOOL 和 _Bool 的區別

      bool 類型在 C++ 中以關鍵字的形式被支持,表示布爾類型,其對應變量的值只有真(true)和假(false)兩種值。

      BOOL 類型在頭文件 <windef.h> 中定義爲 typedef int BOOL;在頭文件 <wtypes.h> 中定義爲 typedef long BOOL;
      BOOL 類型的長度視實際環境來定,通常可認爲是 4 個字節。
      BOOL 是微軟定義的表達布爾邏輯的類型。與 C++ 中的 bool 類型不一樣是,它是一個三值邏輯:TRUE、FALSE 和 ERROR。當返回值爲大於 0 的整數時爲 TRUE,返回值爲 0 時爲 FALSE,返回值爲 -1 時爲 ERROR。

      _Bool 是 C99 標準中定義的一個新關鍵字,以提供布爾類型。C2008 草案中只規定了 _Bool 類型的大小至少應可以存放 0 和 1 這兩個值。而並無規定具體的大小。這交給編譯器自由發揮了。


【跨平臺如何使用布爾類型】

      C++ 裏有專門的 bool 關鍵字。可是在 C99 以前,C 語言裏沒有這樣的類型。從 C99 標準開始,增長了關鍵字 _Bool 用來表示布爾類型。因此只要你的編譯器支持 C99,你就能夠直接使用布爾型了(固然,VC,VS系列編譯器均不支持 C99)。除此以外,C99 爲了在 C 中兼容 C++ 裏對布爾類型的定義,又增長了一個頭文件 stdbool.h。並在其中定義了 bool、true 和 false,讓咱們能夠像 C++ 同樣的定義和使用布爾類型。

使用布爾類型的幾種方式:
a. 本身定義的「仿布爾類型」
在 C99 標準被支持以前,咱們經常本身模仿定義布爾類型,方式有不少種,常見的有下面兩種:

/* 第一種方法 */
typedef int BOOL;
#define TRUE 1
#define FALSE 0

/* 第二種方法 */  html

enum bool{false, true};

b. 使用 C99 新增的關鍵字 _Bool linux

      C99 新增關鍵字 _Bool 類型的長度爲 1,只能取值爲 0 或 1 。將任意非零值賦值給 _Bool 類型變量,都會先轉換爲 1,表示爲真。將零值賦值給 _Bool 類型,結果爲 0,表示爲假。

c. 使用 C99 新增頭文件 stdbool.h
      在 C++ 中,經過 bool 來定義布爾變量,經過 true 和 false 對布爾變量進行賦值。C99 爲了讓咱們可以寫出與 C++ 兼容的代碼,添加了頭文件 <stdbool.h> 。因此咱們只要包含了該頭文件,就能夠像 C++ 中使用布爾變量的方式進行操做。

在我本身的 linux 系統中查找 stdbool.h 頭文件,找到兩處:
1. 系統定義
[root@Betty ~]# vi /usr/lib/syslinux/com32/include/stdbool.h

/*
 * $Id: stdbool.h,v 1.1 2003/04/16 06:32:31 hpa Exp $
 *
 * stdbool.h
 */

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
# if !defined(__GNUC__) ||(__GNUC__ < 3)
  typedef char _Bool;           /* For C compilers without _Bool */
# endif
#endif

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

#else

/* C++ */
#define bool  bool
#define true  true
#define false false

#endif

#define __bool_true_false_are_defined 1

#endif /* _STDBOOL_H */

2. GCC 定義 redis

[root@Betty ~]# vi /usr/lib/gcc/x86_64-redhat-linux/4.1.1/include/stdbool.h

/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */

/* As a special exception, if you include this header file into source
   files compiled by GCC, this header file does not by itself cause
   the resulting executable to be covered by the GNU General Public
   License.  This exception does not however invalidate any other
   reasons why the executable file might be covered by the GNU General
   Public License.  */

/*
 * ISO C Standard:  7.16  Boolean type and values  <stdbool.h>
 */

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

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

#else /* __cplusplus */

/* Supporting <stdbool.h> in C++ is a GCC extension.  */
#define _Bool   bool
#define bool    bool
#define false   false
#define true    true

#endif /* __cplusplus */

/* Signal that all the definitions are present.  */
#define __bool_true_false_are_defined   1

#endif  /* stdbool.h */

其實這兩個頭文件的定義自己還隱形的說明了一些信息,閱者自斟。 this

參考文章:
1.《 淺談C語言中的布爾(bool)類型
2.《 C語言的布爾類型(_Bool)
3.《 C/C++ 中的bool類型 》(這個文章有料)
相關文章
相關標籤/搜索