C语言中不具有原生bool类型

有点标题党了,准确的说是C语言标准中并无bool这个关键字来表示布尔类型。
在C++中我们通常使用bool变量存储逻辑值。
但是,C语言中是没有bool类型的,C语言中只有_Bool类型。
今天和人聊到这个问题,确实容易搞混淆,写出来记录一下。

[ISO/IEC 9899:2011(E) §7.18] Boolean type and values <stdbool.h>

The header <stdbool.h> defines four macros.The macro bool expands to _Bool.
The remaining three macros are suitable for use in #if preprocessing directives. They are true which expands to the integer constant 1,false which expands to the integer constant 0, and __bool_true_false_are_defined which expands to the integer constant 1.
Notwithstanding the provisions of 7.1.3, a program may undefine and perhaps then redefine the macros bool, true, and false.259)

[C Primer Plus 5th P46]_Bool类型由C99引入,用于表示布尔值,即C用值1表示true,用值0表示false,所以_Bool类型也是一种整数类型。只是原则上它们仅仅需要1位来进行存储。

因为对于0和1来说,1bit的存储空间已经够了。
C对真(true)的范围放的非常宽。所有非0的值都被认为是真,只有0被认为是假。这使得判断条件是建立在数值的基础上而不是在真/假的基础上。要谨记如果表达式为真,它的值就为1;如果为假,它的值就为0.因此很多表达式实际上是数值的。——C Primer Plus5th P123
C99提供了一个stdbool.h文件。包含这个头文件就可以使用bool来代替_Bool,并把truefalse定义成值为1和0的符号常量。在程序中包含这个头文件可以写出与C++兼容的代码,因为C++把bool、true和false定义为关键字。——C Primer Plus 5th P125

如果您的系统不支持_Bool,则可以使用int来替代_Bool。——C Primer Plus 5th P125

以下代码用于测试:

1
2
3
4
5
6
7
8
9
// 直接使用bool和flase是错误的
#include <stdio.h>
int main(int argc,char* argv[]){
bool x=false;
if(!x){
printf("x is false\n");
}
return 0;
}

使用gcc编译(gcc -o testbool testbool.c)会产生如下错误:

1
2
3
4
5
6
7
8
testbool.c: In function 'main':
testbool.c:5:2: error: unknown type name 'bool'
bool x=false;
^
testbool.c:5:9: error: 'false' undeclared (first use in this function)
bool x=false;
^
testbool.c:5:9: note: each undeclared identifier is reported only once for each function it appears in

当我们包含stdbool.h之后久不会报错了。

1
#include <stdbool.h>

编译(一定要添加std=c99)运行结果:

1
x is false

我们来看一下stdbool.h中的代码(Visual Studio2015中的):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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 */

下面这个版本是MinGW中的stdbool.h

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
/* Copyright (C) 1998-2015 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 3, 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.

Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.

You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */

/*
* 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 _Bool in C++ is a GCC extension. */
#define _Bool bool

#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension. */
#define bool bool
#define false false
#define true true
#endif

#endif /* __cplusplus */

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

#endif /* stdbool.h */

可以看出来,包含了stdbool.h之后使用的bool实际上也是_Bool类型的,而truefalse也是被预处理器定义为了1和0的字面值常量,所以能够实现使用booltrue以及false关键字的目的,也算是曲线救国吧:)

全文完,若有不足之处请评论指正。

微信扫描二维码,关注我的公众号。

本文标题:C语言中不具有原生bool类型
文章作者:查利鹏
发布时间:2016年06月01日 19时46分
本文字数:本文一共有1.3k字
原始链接:https://imzlp.com/posts/20582/
许可协议: CC BY-NC-SA 4.0
文章禁止全文转载,摘要转发请保留原文链接及作者信息,谢谢!
您的捐赠将鼓励我继续创作!