为什么要使用< stdbool.h>而不是_Bool? [英] Why use <stdbool.h> instead of _Bool?
问题描述
每当我需要布尔类型时,都会被告知要创建一个布尔类型,或者更好的方法是使用stdbool.h
.
Any time I had the need of a Boolean type I was told to either create one, or better yet, use stdbool.h
.
由于stdbool.h
使用typedef bool _Bool
,是否有理由使用标头而不是仅使用类型_Bool
?仅仅是附加的宏(/* #define true 1 #define false 0 */
)吗?
Since stdbool.h
uses typedef bool _Bool
, is there a reason to use the header instead just using type _Bool
? Is it just for the additional macros (/* #define true 1 #define false 0 */
)?
推荐答案
添加到该语言中的明显类型是bool
.但是不幸的是,编写了很多代码,其中包括其他形状和形式的bool
.回想一下,仅在C99中添加了对布尔类型的支持.
The obvious type to add into the language was bool
. But unfortunately, plenty of code was written that included bool
in other shapes and forms. Recall that support for a boolean type was added only in C99.
因此C语言委员会别无选择,只能为其提取保留的标识符(_Bool
).但是,由于类型名称的明显选择仍然相同,因此添加了stdbool.h
以允许用户使用明显名称.这样,如果您的代码没有自制的bool
,则可以使用内置代码.
So the C language committee had no choice but to pull out a reserved identifier for it (_Bool
). But, since the obvious choice of type name is still the same, stdbool.h
was added to allow users the obvious name. That way, if your code didn't have a home-brewed bool
, you could use the built in one.
因此,如果您不绑定到某些现有的自家酿制的bool
,请确实使用stdbool.h
.这将是标准类型,具有所有类型带来的好处.
So do indeed use stdbool.h
if you aren't bound to some existing home-brewed bool
. It will be the standard type, with all the benefits that type brings in.
这篇关于为什么要使用< stdbool.h>而不是_Bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!