C/C++ 中的布尔值是什么?关键字还是宏? [英] What is bool in C/C++? A keyword or a macro?

查看:27
本文介绍了C/C++ 中的布尔值是什么?关键字还是宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提到 this question,其中一些答案表明 bool 是一个整数类型(IDE 也将其视为关键字).

I referred this question, in which some of the answers suggest that bool is an integral type (IDEs also treat it as a keyword).

但是,没有一个答案表明 cplusplus 中提供的信息,这表明bool 是通过 添加的宏(在这种情况下,编译器可能会在编译时隐式添加此标头以允许 bool).这是 <stdbool.h> 的 g++ 版本.

However, none of the answers suggest the information provided in cplusplus, which says that bool is a macro which is added through <cstdbool> (In that case, the compilers might be implicitly adding this header while compiling to allow bool). Here is the g++ version of <stdbool.h>.

那么 bool 究竟是什么?整数类型关键字或宏?

So what exactly the bool is? A an integral type keyword or a macro?

推荐答案

在 C 中,bool 是一个宏.

C中没有内置类型或关键字bool,所以典型的实现使用标准库来#define truefalse 分别为 10.诸如 if 语句的规则是根据零"定义的.和非零"表达式,因此依赖于 truefalse 的扩展宏定义:

In C, bool is a macro.

There is no built-in type or keyword by the name of bool in C, so typical implementations use the standard library to #define true and false to 1 and 0 respectively. Rules such as those for the if statement are defined in terms of "zero" and "non-zero" expressions, and therefore rely on the expanded macro definitions of true and false:

[C99: 6.8.4.1/2]: 在这两种形式中,如果表达式比较不等于 0,则执行第一个子语句.在 else 形式中,如果表达式比较,则执行第二个子语句比较等于 0.如果通过标签到达第一个子语句,则不执行第二个子语句.

[C99: 6.8.4.1/2]: In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal to 0. If the first substatement is reached via a label, the second substatement is not executed.

为方便起见,C99 增加了内置的中间类型 _Bool,这种语言的实现通常是 #define bool_布尔.这种类型是这样定义的:

For convenience, C99 added the built-in intermediate type _Bool, and implementations of this language typically #define bool to _Bool. This type is defined thus:

[C99: 6.2.5/2]: 声明为 _Bool 类型的对象大到足以存储值 0 和 1.

[C99: 6.2.5/2]: An object declared as type _Bool is large enough to store the values 0 and 1.

这允许与 C++ 程序更好地兼容,其中可能包括使用 bool 类型的函数声明;不过,真的,#define _Bool int 可能就足够了.

This allows for greater compatibility with C++ programs, which may include declarations of functions using the bool type; really, though, #define _Bool int would probably have sufficed.

您提供的链接没有说 bool 是 C++ 中的宏.它说:

The link you provided doesn't say that bool is a macro in C++. It says:

这个头文件在C中的目的是添加一个bool类型和真假值作为宏定义.

The purpose in C of this header is to add a bool type and the true and false values as macro definitions.

在直接支持这些类型的 C++ 中,标头仅包含一个宏,可用于检查该类型是否受支持.

In C++, which supports those directly, the header simply contains a macro that can be used to check if the type is supported.

这是正确的.

语义(即,就代码的含义"而言),[C++11: 3.9.1/2] 定义了 bool 在 C++ 中作为整数类型.

Semantically (that is, in terms of "meaning" of your code), [C++11: 3.9.1/2] defines bool as an integral type in C++.

词法(即,就代码中的外观"而言),[C++11: 2.12/1] 将其列为关键字.事实上,作为整数类型名称一部分的all标记也是关键字,包括(但不限于):

Lexically (that is, in terms of "appearance" in your code), [C++11: 2.12/1] lists it as a keyword. In fact, all tokens that are part of the names of integral types are also keywords, including (but not limited to):

  • int
  • 无符号
  • bool
  • 签名

然而,从不是 C++ 中的宏.相反,您会得到一个宏 __bool_true_false_are_defined,您可以在多语言代码中使用它来根据您是使用 C 还是 C++ 来切换对 bool 的处理;请注意,我不确定我能想出一个有用的例子.

It is, however, never a macro in C++. Instead, you get a macro __bool_true_false_are_defined which you could use in multi-language code to switch treatment of bool depending on whether you're working in C or C++; I'm not sure I can think of a useful example, mind you.

这篇关于C/C++ 中的布尔值是什么?关键字还是宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆