C99 - 为什么将 false 和 true 定义为 0 和 1 而不是 ((bool)0) 和 ((bool)1)? [英] C99 - why are false and true defined as 0 and 1 and not as ((bool)0) and ((bool)1)?

查看:22
本文介绍了C99 - 为什么将 false 和 true 定义为 0 和 1 而不是 ((bool)0) 和 ((bool)1)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚偶然发现一个失败的断言,因为它将 false 与函数的返回类型进行比较,因为函数本身返回了一个布尔值,并且断言不仅检查了值,还检查了返回值的类型以匹配那个为假,以保证返回一个布尔值.现在的问题是,C99 将 bool 定义为 _Bool 并且 _Bool 甚至不一定与 int 大小相同(实际上,根据我的经验,在当今的大多数平台上,它通常与 unsigned char 大小相同),更不用说相同了type (这实际上是不可能的,因为 _Bool 是 C99 中语言的内置类型),但将 false 和 true 定义为 0 和 1,没有任何类型转换,并且没有类型转换的预处理器定义将默认为 int.如果 C99 将 false 和 true 定义为 ((bool)0) 和 ((bool)1),那么无论 _Bool 是如何定义的,它们都将始终是 bool 类型.那么是否有充分的理由将它们始终定义为 int,即使 bool 在该平台上不是 int 或者这只是语言中的一个错误,应该用 C1x 修复?

Just stumbled across an assert, that failed, as it compared false to the returntype of a function, as the function itself returned a bool and the assert checked not only the value, but also the type of the returnvalue to match the one of false, to guarantee, that a bool is returned. Now the problem is, that C99 defines bool as _Bool and _Bool is even not necessarily same size as int (in fact, in my experience, on most platforms in nowadays it is often same size as unsigned char), not to talk about being same type (which is actually impossible, as _Bool is a builtin type of the language in C99), but defines false and true as 0 and 1 without any typecast and preprocessor definitions without a typecast will default to int. If C99 would instead define false and true as ((bool)0) and ((bool)1), they would always be of type bool, no matter, how _Bool is defined. So is there any good reason to have them always defined as ints, even when bool is not an int on that platform or is this just a bug in the language, that should be fixed with C1x?

推荐答案

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;

这篇关于C99 - 为什么将 false 和 true 定义为 0 和 1 而不是 ((bool)0) 和 ((bool)1)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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