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

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

问题描述

我提到这个问题,其中一些问题的答案表明,布尔是一个整数类型(集成开发环境也把它当作一个关键字)。

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 提供的信息,它说,布尔是通过℃加入宏; cstdbool> (在这种情况下,编译器可能会隐含加入这个头在编译时允许布尔)。这里的G ++版本 < stdbool.h方式>

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>.

那么究竟是什么布尔是?一个整型的关键字或宏?

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

推荐答案

有没有内置由布尔在C,所以典型的实现使用标准库的#define <名称类型或关键字/ code> 真正 1 0 分别。规则,例如那些用于如果语句中的零和非零前pressions术语被定义,因此,依靠扩大的宏定义真正

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]:在这两种形式中,如果EX pression比较不等于0。在别的执行的第一个子形式,如果前pression如果经由标签达到第一子语句比较等于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 ++程序更好的兼容性,其中可能包括使用布尔键入函数的声明;真的,不过,的#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.

您提供的不说,布尔是在C ++宏的链接。它说:

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

在这个头的C中的目的是增加一个布尔类型和真假值作为宏定义。

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.

这是正确的。

语义(也就是,在你的code的意义而言), [C ++ 11 3.9.1 / 2] 定义布尔在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++.

词法(也就是,在你的code亮相而言), [C ++ 11:2.12 / 1] 将其列为关键字。事实上,的所有的令牌是整型的名字的一部分,也是关键字,包括(但不限于):

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

  • 无符号


  • 布尔


  • 签署

  • int
  • unsigned
  • long
  • bool
  • short
  • signed

有,然而,的从不的在C ++宏。相反,你会得到一个宏 __ bool_true_false_are_defined ,你可以在多语言code使用切换处理布尔的这取决于你用​​C或C ++是否正在努力;我不知道我能想到的一个很好的例子的,你要知道。

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天全站免登陆