为什么C99的bool是宏而不是typedef? [英] Why is C99's bool a macro rather than a typedef?

查看:145
本文介绍了为什么C99的bool是宏而不是typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么C99中引入的布尔类型支持使用预处理器而不是语言本身的功能?具体来说,我们为什么要这么做:

Why does the boolean type support introduced in C99 use the preprocessor rather than the language's own facilities? Specifically, why do we have:

#define bool    _Bool
#define true    1
#define false   0

<stdbool.h>中而不是:

typedef _Bool bool;
enum { false = 0, true = 1 };

我想枚举可以看作是一种品味问题.但是-为什么没有typedef?

I guess the enum can be seen as a matter of taste. But - why not have a typedef?

推荐答案

来自C11规范的第7.18/3节:

From section 7.18/3 of the C11 specification:

其余三个宏适用于 #if 预处理指令.

The remaining three macros are suitable for use in #if preprocessing directives.

规范列出了truefalse__bool_true_false_are_defined.

规范也继续声明(在7.18/4中)程序可能未定义booltruefalse宏.

The specification also continues to state (in 7.18/4) that the bool, true and false macros may be undefined by a program.

关于未定义它们的最后一部分是(我想),因为在发布C99时,许多遗留代码使用了它们自己的定义以及布尔类型和值的变体.因此,它不会使现有代码无效. 因此,它们是宏,因此可以在预处理器条件下使用,因此程序可以未定义它们.

The last part, about undefining them, is (I guess) because of much legacy code when C99 was published used their own definitions and variations of the boolean types and values. So it would not invalidate existing code. So they are macros so they can be used in preprocessor conditions, and so they can be undefined by a program.

这篇关于为什么C99的bool是宏而不是typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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