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

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

问题描述

对面的断言,失败迷迷糊糊的,因为它比假以一个函数的返回类型,函数本身返回一个bool和断言没有检查才有价值,而且还返回值的类型来匹配一假,以保证时,返回一个布尔值。
现在的问题是,C99布尔定义为_Bool和_Bool甚至不一定相同的大小为int(事实上,根据我的经验,在时下大多数平台往往是同样大小的无符号字符),更不用谈是相同的类型(这实际上是不可能的,因为_Bool是一个内嵌式的C99语言),但是定义虚假和0和1真实的,没有任何强制类型转换和preprocessor定义没有类型转换将默认为int。
如果C99将改为定义虚实为((布尔)0)和((布尔)1),他们将永远是bool类型,不管了,_Bool是如何定义的。
那么,有没有让他们始终定义为int,甚至当布尔是不是该平台上一个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?

推荐答案

真正被定义为整型常量 0 1 分别,因为这正是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 :

< SNIP>

< SNIP >

剩下的三个宏适用于#如果 preprocessing指令的使用。他们
  是

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

真正

它扩展成整型常量1,

&LT; SNIP>

< SNIP >

修改的:如下面的评论指出,看来我稍微misinter preTED的问题,我应该提供的标准规定是这样的原因。其中一个原因,我能想到的是,真正应该是在可用#如果 preprocessing指令(从标准报价提到)。

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

究其原因((布尔)0)((布尔)1)不会正常工作#如果 preprocessing指令,是因为标准不答应。在部分的 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 :

控制条件包含的前pression应为整型常量前pression
  不同之处在于:它不得含有石膏;

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

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

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