简体   繁体   English

sizeof(int)<= sizeof(long)<= sizeof(long long)总是如此?

[英]sizeof(int) <= sizeof(long) <= sizeof(long long) always true?

From C standard, int has of at least 16bit, long has of at least 32bit and at least 64bit for long long if any (some platforms may not support). 从C标准来看,int具有至少16位,长度至少为32位且长度至少为64位(如果有的话)(某些平台可能不支持)。 Just wondering if the sentence as title is always true. 只是想知道作为标题的句子是否总是正确的。

No. The standard only defines the minimum ranges for each of those types. 不可以。标准仅定义了每种类型的最小范围。 Conceivably int could have a 16-bit range, but 48 bits of padding, bringing it to 64-bits (8 bytes, if CHAR_BITS == 8), while long is 32-bits (4 bytes). 可以想象int可以具有16位范围,但是48位填充,使其为64位(8字节,如果CHAR_BITS == 8),而长为32位(4字节)。

Of course, this would be silly. 当然,这很愚蠢。 But it's not forbidden, as such. 但这并不是禁止的。

Note, however, that sizeof(char) == 1 , by definition. 但是,请注意, sizeof(char) == 1 ,根据定义。 So sizeof(char) <= sizeof( anything else ) . 所以sizeof(char) <= sizeof( 其他任何东西 )

According to C Programming/Reference Tables , particularly the Table of Data Types : 根据C编程/参考表 ,特别是数据类型表

int ≥ 16 ≥ size of short int≥16≥短的大小

long ≥ 32 ≥ size of int 长≥32≥int的大小

long long ≥ 64 ≥ size of long 长长≥64尺寸长

As bdonlan pointed out, this only refers to the range of the values, not the size in memory (which sizeof returns in bytes). 正如bdonlan所指出的,这只是指值的范围,而不是内存中的大小(以字节为单位返回sizeof)。 The C standard doesn't specify the size in memory that each type can use, so it's left to the implementation. C标准没有指定每种类型可以使用的内存大小,因此它留给实现。

Practical C++ Programming says that 实用的C ++编程说明了这一点

C++ guarantees that the storage for short <= int <= long C ++保证存储<= int <= long

Still searching for long long . 还在寻找很久

At least for ISO C++, this is well-defined (excepting long long for obvious reasons) by the Standard in 3.9.1[basic.fundamental]/2: 至少对于ISO C ++,这在3.9.1 [basic.fundamental] / 2中的标准中是明确定义的(除了显而易见的long long

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list. 有四种有符号整数类型:“signed char”,“short int”,“int”和“long int”。在此列表中,每种类型至少提供与列表中前面的存储一样多的存储空间

Note that this is speaking of storage, not value ranges. 请注意,这是存储,而不是值范围。 This specifically means sizeof . 这特别意味着sizeof

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM