#include <stdio.h>
#include <stdbool.h>
int main (void) {
bool u, t = true, f = false;
if (t) puts("t is true"); else puts("t is false");
if (f) puts("f is true"); else puts("f is false");
if (u) puts("u is true"); else puts("u is false");
return 0;
}
prog.c: In function ‘main’:
prog.c:8:8: warning: ‘u’ is used uninitialized in this function [-Wuninitialized]
8 | if (u) puts("u is true"); else puts("u is false");
| ^
(1) 丑陋,是的,但它确保了代码之间不会发生冲突可能已被写入在这成为标准的一部分之前,因为以 _X 开头的名称(其中 X 是任何大写字母或另一个 _)被保留给执行。因此你不应该使用它们。
You can very much define a boolean variable without a value, as evidenced by the following code (but note the second header include, see below for explanation):
#include <stdio.h>
#include <stdbool.h>
int main (void) {
bool u, t = true, f = false;
if (t) puts("t is true"); else puts("t is false");
if (f) puts("f is true"); else puts("f is false");
if (u) puts("u is true"); else puts("u is false");
return 0;
}
This outputs (for me):
t is true
f is false
u is false
However, you should be aware of the normal rules regarding initialisation of variables where you don't make it explicit. In the code given, u will hold some arbitrary value and, indeed, using a higher warning level of gcc makes that clear:
prog.c: In function ‘main’:
prog.c:8:8: warning: ‘u’ is used uninitialized in this function [-Wuninitialized]
8 | if (u) puts("u is true"); else puts("u is false");
| ^
Note that, if you want to use bool, true, and false, these are not keywords, they're instead defined in the stdbool header file so you need to include that.
Otherwise all you get is the _Bool type(1) and neither of the nice constants, so you'll have to define some yourself or use 0 and 1 (although any non-zero value converts to 1).
Since this is scarcely a step up for doing the same thing with int, you probably want to do it properly (i.e., use the header).
Note further that this is only available as of C99. The presence of an #include <conio.h> in your code may indicate that you're using a hideously outdated implementation of C (it was a common header for DOS-era compilers such as Turbo C).
If that is the case, it's likely you don't even have a stdbool header that you can use, so you'll have to stick with int, 0, and 1.
Alternatively, you may be able to create your own my_stdbool.h such as the following, assuming there's no naming conflict as previously mentioned (most code I've seen that defines the constants tends to use the upper-case FALSE and TRUE in keeping with commonly-used guidelines):
(1) Uglier, yes, but it ensures there's no clash between code that may have been written before this became part of the standard since names beginning with _X (where X is any upper-case letter or another _) are reserved for the implementation. Hence you shouldn't be using them.
发布评论
评论(3)
要在 C 中使用布尔变量,您需要在标头中添加
#include
。To use Boolean variables in C you need to
#include <stdbool.h>
in your headers.您可以定义一个没有值的布尔变量,如以下代码所示(但请注意第二个标头包含,请参阅下面的解释):
此输出(对我而言):
但是,您应该了解有关的正常规则在没有明确说明的情况下初始化变量。在给出的代码中,
u
将保留一些任意值,事实上,使用更高警告级别的gcc
可以清楚地表明这一点:请注意,如果您想使用
bool
、true
和false
,这些不是关键字,而是已定义的关键字在stdbool
头文件中,因此您需要包含它。否则,您得到的只是
_Bool
类型(1),并且没有任何好的常量,因此您必须自己定义一些或使用0
和1
(尽管任何非零值都会转换为1
)。由于这几乎不是使用
int
执行相同操作的一个步骤,因此您可能希望正确执行此操作(即使用标头)。进一步注意,这仅在 C99 中可用。代码中存在
#include
可能表明您正在使用过时的 C 实现(它是 DOS 的常见标头) -era 编译器,例如 Turbo C)。如果是这种情况,很可能您甚至没有可以使用的
stdbool
标头,因此您必须坚持使用包含int
、0
和1
。或者,您可以创建自己的
my_stdbool.h
,如下所示,假设没有前面提到的命名冲突(我见过的定义常量的大多数代码倾向于使用大写) caseFALSE
和TRUE
与常用准则保持一致):(1) 丑陋,是的,但它确保了代码之间不会发生冲突可能已被写入在这成为标准的一部分之前,因为以
_X
开头的名称(其中X
是任何大写字母或另一个_
)被保留给执行。因此你不应该使用它们。You can very much define a boolean variable without a value, as evidenced by the following code (but note the second header include, see below for explanation):
This outputs (for me):
However, you should be aware of the normal rules regarding initialisation of variables where you don't make it explicit. In the code given,
u
will hold some arbitrary value and, indeed, using a higher warning level ofgcc
makes that clear:Note that, if you want to use
bool
,true
, andfalse
, these are not keywords, they're instead defined in thestdbool
header file so you need to include that.Otherwise all you get is the
_Bool
type(1) and neither of the nice constants, so you'll have to define some yourself or use0
and1
(although any non-zero value converts to1
).Since this is scarcely a step up for doing the same thing with
int
, you probably want to do it properly (i.e., use the header).Note further that this is only available as of C99. The presence of an
#include <conio.h>
in your code may indicate that you're using a hideously outdated implementation of C (it was a common header for DOS-era compilers such as Turbo C).If that is the case, it's likely you don't even have a
stdbool
header that you can use, so you'll have to stick withint
,0
, and1
.Alternatively, you may be able to create your own
my_stdbool.h
such as the following, assuming there's no naming conflict as previously mentioned (most code I've seen that defines the constants tends to use the upper-caseFALSE
andTRUE
in keeping with commonly-used guidelines):(1) Uglier, yes, but it ensures there's no clash between code that may have been written before this became part of the standard since names beginning with
_X
(whereX
is any upper-case letter or another_
) are reserved for the implementation. Hence you shouldn't be using them.在 C 中,您可以使用 _Bool 类型声明布尔变量。以下为例。但是,这不是推荐的方法。
您可以进一步使用宏将
_Bool
定义为bool
。In C, you can declare a boolean variable using the _Bool type. Following as an example. However, it is not a recommended method.
You can further use a macro to define
_Bool
asbool
.