long 和 int 通常大小相同的历史背景是什么?
根据此处的众多答案,long
和int
是在 C 和 C++ 的通用平台(Windows 和 Linux,32 和 64 位)上,大小都是 32 位。(我知道没有标准,但实际上,这些是观察到的大小。)
所以我的问题是,这是怎么发生的?为什么我们有两种尺寸相同的类型?我以前总是假设 long
大多数时候是 64 位,而 int
是 32 位。我并不是说它“应该”是一种方式或另一种方式,我'我只是好奇我们是怎么来到这里的。
According to numerous answers here, long
and int
are both 32 bits in size on common platforms in C and C++ (Windows & Linux, 32 & 64 bit.) (I'm aware that there is no standard, but in practice, these are the observed sizes.)
So my question is, how did this come about? Why do we have two types that are the same size? I previously always assumed long
would be 64 bits most of the time, and int
32. I'm not saying it "should" be one way or the other, I'm just curious as to how we got here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 C99 基本原理 (PDF) 第 6.2.5 节:
From the C99 rationale (PDF) on section 6.2.5:
从历史上看,C 中的大多数尺寸和类型都可以追溯到 PDP-11 架构。它有字节、字(16 位)和双字(32 位)。当C和UNIX被转移到另一台机器(我认为是Interdata 832)时,字长是32位。为了保持源代码兼容,定义了
long
和int
,以便严格现在大多数机器都以
sizeof(int)
=sizeof(long)
结束,因为 16 位不再方便,但如果需要的话,我们还有很长的时间可以得到 64 位。更新 严格来说我应该说“编译器”,因为不同的编译器实现者可以对相同的指令集架构做出不同的决定。例如,海湾合作委员会和微软。
Historically, most of the sizes and types in C can be traced back to the PDP-11 architecture. That had bytes, words (16 bits) and doublewords (32 bits). When C and UNIX were moved to another machine (the Interdata 832 I think), the word length was 32 bits. To keep the source compatible,
long
andint
were defined so that, strictlyMost machines now end up with
sizeof(int)
=sizeof(long)
because 16 bits is no longer convenient, but we have long long to get 64 bits if needed.Update strictly I should have said "compilers" because different compiler implmentors can make different decisions for the same instruction set architecture. GCC and Microsoft, for example.
早在 70 年代末和 80 年代初,许多架构都是 16 位,因此通常 char 是 8 位,int 是 16 位,long 是 32 位。在 80 年代末,普遍转向 32 位架构,因此 int 变成了 32 位,但长期保持在 32 位。
在过去 10 年里,出现了向 64 位计算发展的趋势,我们现在有几种不同的模型,最常见的是 LP64,其中 int 仍然是 32 位,long 现在是 64 位。
底线:不要对不同整数类型的大小做出任何假设(当然标准中定义的除外),如果您需要固定大小的类型,请使用
。Back in the late 70s and early 80s many architectures were 16 bit, so typically char was 8 bit, int was 16 bit and long was 32 bit. In the late 80s there was a general move to 32 bit architectures and so int became 32 bits but long remained at 32 bits.
Over the last 10 years there has been a move towards 64 bit computing and we now have a couple of different models, the most common being LP64, where ints are still 32 bits and long is now 64 bits.
Bottom line: don't make any assumptions about the sizes of different integer types (other than what's defined in the standard of course) and if you need fixed size types then use
<stdint.h>
.据我了解,C 标准要求 long 至少为 32 位长,并且至少与 int 一样长。另一方面,
int
总是(我认为)等于架构的本机字大小。请记住,在制定标准时,32 位计算机并不常见;最初,
int
可能是本机 16 位,而long
在 32 位时长度会是原来的两倍。As I understand it, the C standard requires that a
long
be at least 32 bits long, and be at least as long as anint
. Anint
, on the other hand, is always (I think) equal to the native word size of the architecture.Bear in mind that, when the standards were drawn up, 32-bit machines were not common; originally, an
int
would probably have been the native 16-bits, and along
would have been twice as long at 32-bits.在16位操作系统中,int是16位,long是32位。迁移到 Win32 后,两者都变为 32 位。迁移到 64 位操作系统时,最好保持 long 大小不变,这样在 64 位编译时不会破坏现有代码。新类型(如 Microsoft 特定的 __int64、size_t 等)可以在 64 位程序中使用。
In 16-bit operating systems, int was 16-bit and long was 32-bit. After moving to Win32, both become 32 bit. Moving to 64 bit OS, it is a good idea to keep long size unchanged, this doesn't break existing code when it compiled in 64 bit. New types (like Microsoft-specific __int64, size_t etc.) may be used in 64 bit programs.