C语言 为什么 C 编译器将 long 指定为 32 位,将 long long 指定为 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7279504/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:34:18  来源:igfitidea点击:

Why do C compilers specify long to be 32-bit and long long to be 64-bit?

c32bit-64bitlong-integerbitlong-long

提问by sj755

Wouldn't it have made more sense to make long 64-bit and reserve long long until 128-bit numbers become a reality?

在 128 位数字成为现实之前,制作长 64 位并保留很长时间不是更有意义吗?

回答by Mysticial

Yes, it does make sense, but Microsoft had their own reasons for defining "long" as 32-bits.

是的,这确实有道理,但微软有自己的理由将“长”定义为 32 位。

As far as I know, of all the mainstream systems right now, Windows is the only OS where "long" is 32-bits. On Unix and Linux, it's 64-bit.

据我所知,目前在所有主流系统中,Windows 是唯一一个“长”为 32 位的操作系统。在 Unix 和 Linux 上,它是 64 位的。

All compilers for Windows will compile "long" to 32-bits on Windows to maintain compatibility with Microsoft.

Windows 的所有编译器都会在 Windows 上将“长”编译为 32 位,以保持与 Microsoft 的兼容性。

For this reason, I avoid using "int" and "long". Occasionally I'll use "int" for error codes and booleans (in C), but I never use them for any code that is dependent on the size of the type.

出于这个原因,我避免使用“int”和“long”。有时我会使用“int”来表示错误代码和布尔值(在 C 中),但我从不将它们用于任何依赖于类型大小的代码。

回答by Jason Kuang

The c standard have NOT specified the bit-length of primitive data type, but only the least bit-length of them. So compilers can have options on the bit-length of primitive data types. On deciding the bit-length of each primitive data type, the compiler designer should consider the several factors, including the computer architecture.

c 标准没有规定原始数据类型的位长,而只规定了其中的最小位长。所以编译器可以选择原始数据类型的位长。在决定每个原始数据类型的位长时,编译器设计者应该考虑几个因素,包括计算机体系结构。

here is some references: http://en.wikipedia.org/wiki/C_syntax#Primitive_data_types

这是一些参考资料:http: //en.wikipedia.org/wiki/C_syntax#Primitive_data_types

回答by paulsm4

For historical reasons. For a long time (pun intended), "int" meant 16-bit; hence "long" as 32-bit. Of course, times changed. Hence "long long" :)

由于历史原因。长期以来(双关语),“int”表示 16 位;因此“长”为 32 位。当然,时代变了。因此“长长”:)

PS:

PS:

GCC (and others) currently support 128 bit integers as "(u)int128_t".

GCC(和其他)目前支持 128 位整数作为“(u)int128_t”。

PPS:

缴费灵:

Here's a discussion of why the folks at GCC made the decisions they did:

以下是关于海湾合作委员会的人做出他们所做决定的原因的讨论:

http://www.x86-64.org/pipermail/discuss/2005-August/006412.html

http://www.x86-64.org/pipermail/discuss/2005-August/006412.html

回答by supercat

Ever since the days of the first C compiler for a general-purpose reprogrammable microcomputer, it has often been necessary for code to make use of types that held exactly 8, 16, or 32 bits, but until 1999 the Standard didn't explicitly provide any way for programs to specify that. On the other hand, nearly all compilers for 8-bit, 16-bit, and 32-bit microcomputers define "char" as 8 bits, "short" as 16 bits, and "long" as 32 bits. The only difference among them is whether "int" is 16 bits or 32.

自从第一个用于通用可重编程微型计算机的 C 编译器出现以来,代码经常需要使用恰好保存 8、16 或 32 位的类型,但直到 1999 年标准才明确规定程序指定的任何方式。另一方面,几乎所有 8 位、16 位和 32 位微机的编译器都将“char”定义为 8 位,“short”定义为 16 位,“long”定义为 32 位。它们之间唯一的区别是“int”是 16 位还是 32 位。

While a 32-bit or larger CPU could use "int" as a 32-bit type, leaving "long" available as a 64-bit type, there is a substantial corpus of code which expects that "long" will be 32 bits. While the C Standard added "fixed-sized" types in 1999, there are other places in the Standard which still use "int" and "long", such as "printf". While C99 added macros to supply the proper format specifiers for fixed-sized integer types, there is a substantial corpus of code which expects that "%ld" is a valid format specifier for int32_t, since it will work on just about any 8-bit, 16-bit, or 32-bit platform.

虽然 32 位或更大的 CPU 可以将“int”用作 32 位类型,而将“long”用作 64 位类型,但有大量代码库预计“long”将是 32 位。虽然 C 标准在 1999 年添加了“固定大小”类型,但标准中还有其他地方仍然使用“int”和“long”,例如“printf”。虽然 C99 添加了宏来为固定大小的整数类型提供正确的格式说明符,但有大量代码预计“%ld”是 int32_t 的有效格式说明符,因为它几乎可以用于任何 8 位、16 位或 32 位平台。

Whether it makes more sense to have "long" be 32 bits, out of respect for an existing code base going back decades, or 64 bits, so as to avoid the need for the more verbose "long long" or "int64_t" to identify the 64-bit types is probably a judgment call, but given that new code should probably favor the use of specified-size types when practical, I'm not sure I see a compelling advantage to making "long" 64 bits unless "int" is also 64 bits (which will create even bigger problems with existing code).

出于对可追溯到几十年前的现有代码库的尊重,将“long”设为 32 位更有意义,还是 64 位,以避免需要更冗长的“long long”或“int64_t”来识别64 位类型可能是一个判断调用,但考虑到新代码可能应该在实际中支持使用指定大小的类型,我不确定我是否看到制作“长”64 位的显着优势,除非“int”也是 64 位(这会对现有代码造成更大的问题)。

回答by aarif

d 32-bit microcomputers define "char" as 8 bits, "short" as 16 bits, and "long" as 32 bits. The only difference among them is whether "int" is 16 bits or 32.

d 32 位微机将“char”定义为 8 位,“short”定义为 16 位,“long”定义为 32 位。它们之间的唯一区别是“int”是 16 位还是 32 位。

While a 32-bit or larger CPU could use "int" as a 32-bit type, leaving "long" available as a 64-bit type, there is a substantial corpus of code which expects that "long" will be 32 bits. While the C Standard added "fixed-sized" types in 1999, there are other places in the Standard which still use "int" and "long", such as "printf". While C99 added macros to supply the

虽然 32 位或更大的 CPU 可以将“int”用作 32 位类型,而将“long”用作 64 位类型,但有大量代码库预计“long”将是 32 位。虽然 C 标准在 1999 年添加了“固定大小”类型,但标准中还有其他地方仍然使用“int”和“long”,例如“printf”。虽然 C99 添加了宏来提供