微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C 11标准中的哪一节规定了原始数据类型大小之间的相对排序?

我试图找出C标准是否指定了各种类型的大小之间的关系.例如,https://stackoverflow.com/a/589599/1175080的答案似乎声称:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

https://stackoverflow.com/a/589684/1175080的另一个答案有类似的说法:

sizeof(int) <= sizeof(long)

我正在经历n3337.pdf(我相信C 11标准的良好代理)但我找不到保证这些不等式的特定语言.

n1256(C99)中,我可以在5.2.4.2.1尺寸中找到特定的语言
整数类型清楚地说明每种类型的最小值和最大值,间接确定大小之间的相对排序.

这些不等式是否在C标准中类似地定义,还是直接从C继承?标准中的语言在哪里?

解决方法

在n3337中,该部分是 3.9.1,[basic.fundamental]/2,第二段(重点是我的):

There are five standard signed integer types : “signed char”,“short int”,“int”,“long int”,and “long long int”. In this list,each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types. Plain ints have the natural size suggested by the architecture of the execution environment44; the other signed integer types are provided to meet special needs.

请注意,C标准中的5.2.4.2/1仅定义了每个整数类型的最小范围,它不强制执行排序 – 我可以从-32767到32767使用int,从-2147483647到2147483647使用,并且仍然符合本节的要求.

但是6.2.5.8更加明确:

For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1),the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

6.3.1.1告诉你:

The rank of long long int shall be greater than the rank of long int,which shall be greater than the rank of int,which shall be greater than the rank of short int,which shall be greater than the rank of signed char.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐