【原创】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 标准被支持之前,我们常常自己模仿定义布尔类型,方式有很多种,常见的有下面两种:

/* 第一种方法 */  
?
1
2
3
typedef int BOOL ;
#define TRUE 1
#define FALSE 0

/* 第二种方法 */ 

?
1
enum bool { false , true };

b. 使用 C99 新增的关键字 _Bool

      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. 系统定义  
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[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 定义

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[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 */

其实这两个头文件的定义本身还隐形的说明了一些信息,阅者自斟。

目录
相关文章
|
6月前
|
C语言 C++
C++ bool 类型
C++ bool 类型
50 0
|
7月前
const int*p 与 int const *p与 int * const p与const int * const p的区别(有明显对比,超级详细,超级好记)
当只有一个const并且const在*左边时: (const int*p 与 int const *p,)*p的值不能改,但是p(地址)能改.我们可以看到第一,三张图片第五行*p下面有红线表示错误.而第二,四张没有红线的地方表示正确.所以我们可知道const int*p 与 int const *p是相同的只是书写方法不同.
36 0
|
8月前
|
C++
C++ int const 和 const int 的区别
C++ int const 和 const int 的区别
|
8月前
|
前端开发
return false得使用
return false得使用
31 0
|
8月前
bool与引用类型
bool与引用类型
|
Python
Python类型转换的四个函数int()、float()、str()、bool()
Python类型转换的四个函数int()、float()、str()、bool()自制脑图 将一个类型的对象转换为其他对象类型转换不是改变对象本身的类型,而是根据当前对象的值创建一个新对象。
199 0
Python类型转换的四个函数int()、float()、str()、bool()
C++类中int getX( ) const {return x;}含义
C++类中int getX( ) const {return x;}含义
184 0
|
C语言
C - C 语言如何才能使用 bool 类型
C - C 语言如何才能使用 bool 类型
76 0
|
C++ 算法 C语言
VC之CString,wchar_t,int,string,char*之间的转换
    VC之CString,wchar_t,int,string,char*之间的转换 1. CString 转 wchar_t CString path = "asdf"; wchar_t wstr[256] = path.
2808 0
|
C语言
[C]C语言中return 0和return 1和return -1
1、返回值int 类型的函数返回: return语句用来结束循环,或返回一个函数的值。 return 0:一般用在主函数结束时,按照程序开发的一般惯例,表示成功完成本函数。
3394 0