C语言 为什么 sizeof(int) == sizeof(long)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18901080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the sizeof(int) == sizeof(long)?
提问by ChrisMcJava
In the program listed below, the sizeof(int) and sizeof(long) are equal on my machine (both equal 4 bytes (or 32 bits)). A long, as far as I know, is 8 bytes. Is this correct? I have a 64-bit machine
在下面列出的程序中,sizeof(int) 和 sizeof(long) 在我的机器上是相等的(都等于 4 个字节(或 32 位))。据我所知,long 是 8 个字节。这样对吗?我有一台 64 位机器
#include <stdio.h>
#include <limits.h>
int main(void){
printf("sizeof(short) = %d\n", (int)sizeof(short));
printf("sizeof(int) = %d\n", (int)sizeof(int));
printf("sizeof(long) = %d\n", (int)sizeof(long));
printf("sizeof(float) = %d\n", (int)sizeof(float));
printf("sizeof(double) = %d\n", (int)sizeof(double));
printf("sizeof(long double) = %d\n", (int)sizeof(long double));
return 0;
}
回答by Reed Copsey
A long, as far as I know, is 8 bytes. Is this correct?
据我所知,long 是 8 个字节。这样对吗?
No, this is not correct. The C and C++ specifications only state that long must be greater than or equal to 32 bits. intcan be smaller, but on many platforms, in C and C++, longand intare both 32 bits.
不,这是不正确的。C 和 C++ 规范仅声明 long 必须大于或等于 32 位。 int可以更小,但在许多平台上,在 C 和 C++ 中,long并且int都是 32 位。
This is a very good reason to prefer fixed width integer typessuch as int64_tif they're available to you and you're using C99 or a framework which provides you an equivalent type.
这是首选固定宽度整数类型的一个很好的理由,例如int64_t如果它们可供您使用并且您使用的是 C99 或为您提供等效类型的框架。
回答by chmeee
It depends on your ABI. If you are using Windows, Microsoft chose a LLP64 model, so longand intare both 32-bits, while long longand pointers are 64-bits in 64-bit builds, for legacy reasons.
这取决于您的 ABI。如果您使用的是 Windows,Microsoft 选择了 LLP64 模型,因此long和int都是 32 位,而long long和 指针在 64 位版本中是 64 位,这是出于遗留原因。
Most UNIX platforms chose a LP64 model, which makes int32-bits, long, long long, and pointers 64-bits, for 64-bit builds.
大多数 UNIX 平台选择 LP64 模型,该模型使int32 位long、long long、 和 64 位指针成为 64 位构建。
But, as Reed Copsey notes, the standard only states minimum lengths, and relative lengths. And longmust be greater than or equal to the length of int.
但是,正如 Reed Copsey 指出的那样,该标准仅规定了最小长度和相对长度。并且long必须大于或等于 的长度int。
回答by Palec
C is not Java nor C#. As others wrote, C spec states minimum lengths and relative lengths. Why? C was designed to be low-level and to compile and run on virtually any hardware ever made.
C 不是 Java 也不是 C#。正如其他人所写,C 规范规定了最小长度和相对长度。为什么?C 被设计为低级的,并且可以在几乎任何制造过的硬件上编译和运行。
If a spec promises the programmer too much, its implementation can get tricky (and slow) when hardware does not support such a thing. Java and C# developers don't care too much, they like the convenience of their languages for higher-level jobs and don't hesitate to install large virtual machines that take care of fulfilling all the promises (or at least most of them).
如果规范向程序员承诺太多,当硬件不支持这样的事情时,它的实现可能会变得棘手(和缓慢)。Java 和 C# 开发人员不太关心,他们喜欢他们的语言为更高级别的工作提供的便利,并且毫不犹豫地安装大型虚拟机来完成所有承诺(或至少大部分)。
C programmers want to have control over the code even on machine instruction level. Complete control over the hardware. Only this way can all hardware features be used, and maximum performance be reached. But you have to be careful with your assumptions about the hardware you are using. As always, with great power comes great responsibility.
C 程序员甚至希望在机器指令级别控制代码。完全控制硬件。只有这样才能使用所有硬件功能,并达到最大性能。但是你必须小心你对所使用硬件的假设。一如既往,强大的力量伴随着巨大的责任。
Just to illustrate that: C does not assume 8-bit bytes. See CHAR_BITin <limits.h>.
只是为了说明:C 不假设 8 位字节。见CHAR_BIT在<limits.h>。
A related question is Size of an integer in C.
一个相关的问题是C 中整数的大小。
回答by Shafik Yaghmour
No, the standard defines the minimums which for longis 4 bytesand intis 2 bytes. As per the C99 draft standardsection 5.2.4.2.1Sizes of integer types paragraph 1says:
不,标准定义了longis4 bytes和intis的最小值2 bytes。根据C99 草案标准部分5.2.4.2.1整数类型的大小第1段说:
[...]Their implementation-defined values shall be equal or greater in magnitude[...]
[...]它们的实现定义的值在数量上应相等或更大[...]
and for longwe have:
因为long我们有:
LONG_MIN -2147483647 // -(2^31 - 1)
LONG_MAX +2147483647 // 2^31 - 1
which is 4 bytes. For completeness sake we have the following for int:
这是4 bytes. 为了完整起见,我们有以下内容int:
INT_MIN -32767 // -(2^15 - 1)
INT_MAX +32767 // 2^15 - 1
which is 2 bytes.
这是2 bytes.

