【问题标题】:Why is C99's bool a macro rather than a typedef?为什么 C99 的 bool 是宏而不是 typedef?
【发布时间】:2018-08-22 23:37:10
【问题描述】:

为什么 C99 中引入的布尔类型支持使用预处理器而不是语言自己的工具?具体来说,为什么我们有:

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

<stdbool.h> 而不是:

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

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

【问题讨论】:

    标签: boolean c99 stdbool


    【解决方案1】:

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

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

    规范列出了truefalse__bool_true_false_are_defined

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-10
      • 1970-01-01
      • 2014-10-06
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多