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

It seems a bit clickbaity; to be precise, there is no bool keyword in the C language standard to represent boolean types. In C++, we usually use bool variables to store logical values. However, there is no bool type in C; C only has the _Bool type. Today, when discussing this issue with someone, it can indeed be confusing, so I’m writing it down for future reference.

[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 type was introduced with C99 to represent boolean values, meaning C uses the value 1 to represent true and the value 0 to represent false; thus, the _Bool type is also an integer type. In principle, they only need 1 bit for storage.

Because for 0 and 1, 1 bit of storage is sufficient. C is very liberal about the definition of true. All non-zero values are considered true, and only 0 is considered false. This means that condition checking is based on numerical values rather than on true/false values. It’s important to remember that if an expression is true, its value is 1; if false, its value is 0. Therefore, many expressions are essentially numerical. — C Primer Plus 5th P123
C99 provides a stdbool.h file. Including this header file allows the use of bool in place of _Bool, with true and false defined as symbolic constants with values 1 and 0. Including this header in the program allows writing code that is compatible with C++, as C++ defines bool, true, and false as keywords. — C Primer Plus 5th P125

If your system does not support _Bool, you can use int as a substitute for _Bool. — C Primer Plus 5th P125

The following code is for testing:

1
2
3
4
5
6
7
8
9
// Directly using bool and false is incorrect
#include <stdio.h>
int main(int argc, char* argv[]) {
bool x = false;
if (!x) {
printf("x is false\n");
}
return 0;
}

Compiling with gcc (gcc -o testbool testbool.c) will produce the following error:

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

When we include stdbool.h, it will no longer report errors.

1
#include <stdbool.h>

Compile (make sure to add -std=c99), and the result is:

1
x is false

Let’s take a look at the code in stdbool.h (in Visual Studio 2015):

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 */

The following version is from MinGW’s 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 */

It can be seen that after including stdbool.h, the bool used is actually of _Bool type, and true and false are also defined as literal constants 1 and 0 by the preprocessor, allowing the use of bool, true, and false keywords, which could be seen as a roundabout way to address the issue :)

The article is finished. If you have any questions, please comment and communicate.

Scan the QR code on WeChat and follow me.

Title:C语言中不具有原生bool类型
Author:LIPENGZHA
Publish Date:2016/06/01 19:46
World Count:2.2k Words
Link:https://en.imzlp.com/posts/20582/
License: CC BY-NC-SA 4.0
Reprinting of the full article is prohibited.
Your donation will encourage me to keep creating!