且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C99 - 为什么将 false 和 true 定义为 0 和 1 而不是 ((bool)0) 和 ((bool)1)?

更新时间:2022-05-23 21:33:44

falsetrue 被定义为整数常量 01 分别,因为这正是 C99 标准在 7.16 节中指定的内容:

false and true are defined as the integer constants 0 and 1 respectively, because that's exactly what the C99 standard specifies in section 7.16 :

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

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

展开成整数常数1,

展开成整数常量 0,并且

which expands to the integer constant 0, and

EDIT :正如下面的评论所示,我似乎稍微误解了这个问题,我应该提供标准这样指定的原因.我能想到的一个原因是 truefalse 应该可以在 #if 预处理指令中使用(作为标准的引用提及).

EDIT : as the comments below indicate, it seems I slightly misinterpreted the question, and I should have provided the reason the standard specifies it like that. One reason I can think of, is that true and false are supposed to be usable in #if preprocessing directives (as the quote from the standard mentions).

((bool) 0)((bool) 1)#if 预处理指令中不起作用的原因是因为标准不允许.在 6.10.1 部分它说:

The reason ((bool) 0) or ((bool) 1) won't work in #if preprocessing directives, is because the standard doesn't allow it. In section 6.10.1 it says :

控制条件包含的表达式应为整数常量表达式除了:它不应包含演员表;

The expression that controls conditional inclusion shall be an integer constant expression except that: it shall not contain a cast;