C++ long 和 int 数据类型的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/900230/
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
Difference between long and int data types
提问by Alex
Considering that the following statements return 4
, what is the difference between the int
and long
types in C++?
考虑到以下语句 return 4
,C++ 中的int
和long
类型有什么区别?
sizeof(int)
sizeof(long)
回答by Paul Sonier
From thisreference:
从这个参考:
An int was originally intended to be the "natural" word size of the processor. Many modern processors can handle different word sizes with equal ease.
int 最初旨在成为处理器的“自然”字长。许多现代处理器可以同样轻松地处理不同的字长。
Also, this bit:
另外,这一点:
On many (but not all) C and C++ implementations, a long is larger than an int. Today's most popular desktop platforms, such as Windows and Linux, run primarily on 32 bit processors and most compilers for these platforms use a 32 bit int which has the same size and representation as a long.
在许多(但不是全部)C 和 C++ 实现中,long 大于 int。当今最流行的桌面平台,例如 Windows 和 Linux,主要在 32 位处理器上运行,并且这些平台的大多数编译器使用 32 位 int,其大小和表示形式与 long 相同。
回答by Dan Olson
The guarantees the standard gives you go like this:
标准给你的保证是这样的:
1 == sizeof(char) <= sizeof(short) <= sizeof (int) <= sizeof(long) <= sizeof(long long)
So it's perfectly valid for sizeof (int)
and sizeof (long)
to be equal, and many platforms choose to go with this approach. You will find some platforms where int
is 32 bits, long
is 64 bits, and long long
is 128 bits, but it seems very common for sizeof (long)
to be 4.
所以它完全有效sizeof (int)
并且sizeof (long)
是平等的,许多平台选择采用这种方法。你会发现一些平台int
有 32 位、long
64 位和long long
128 位,但sizeof (long)
4位似乎很常见。
(Note that long long
is recognized in C from C99 onwards, but was normally implemented as an extension in C++ prior to C++11.)
(请注意,long long
从 C99 开始,它在 C中被识别,但通常在 C++11 之前作为 C++ 中的扩展实现。)
回答by Jonathan Leffler
You're on a 32-bit machine or a 64-bit Windows machine. On my 64-bit machine (running a Unix-derivative O/S, not Windows), sizeof(int) == 4
, but sizeof(long) == 8
.
您使用的是 32 位计算机或 64 位 Windows 计算机。在我的 64 位机器上(运行 Unix 衍生操作系统sizeof(int) == 4
,而不是 Windows),, 但是sizeof(long) == 8
.
They're different types — sometimes the same size as each other, sometimes not.
它们是不同的类型——有时大小相同,有时不同。
(In the really old days, sizeof(int) == 2
and sizeof(long) == 4
— though that might have been the days before C++ existed, come to think of it. Still, technically, it is a legitimate configuration, albeit unusual outside of the embedded space, and quite possibly unusual even in the embedded space.)
(在真正的过去,sizeof(int) == 2
以及sizeof(long) == 4
-尽管这可能是以前的C ++存在的几天来,对想起来还,在技术上,它是一个合法的配置,虽然在嵌入式领域的不同寻常之外,相当甚至可能是不寻常的。嵌入空间。)
回答by JaredPar
On platforms where they both have the same size the answer is nothing. They both represent signed 4 byte values.
在它们都具有相同大小的平台上,答案是什么。它们都表示有符号的 4 字节值。
However you cannot depend on this being true. The size of long and int are not definitively defined by the standard. It's possible for compilers to give the types different sizes and hence break this assumption.
然而,你不能依赖这是真的。标准没有明确定义 long 和 int 的大小。编译器可以给类型不同的大小,从而打破这个假设。
回答by abelenky
The long must be at least the same size as an int, and possibly, but not necessarily, longer.
long 必须至少与 int 大小相同,并且可能(但不一定)更长。
On common 32-bit systems, both int and long are 4-bytes/32-bits, and this is valid according to the C++ spec.
在常见的 32 位系统上,int 和 long 都是 4 字节/32 位,根据 C++ 规范这是有效的。
On other systems, both int and long long may be a different size. I used to work on a platform where int was 2-bytes, and long was 4-bytes.
在其他系统上, int 和 long long 可能是不同的大小。我曾经在 int 为 2 字节,long 为 4 字节的平台上工作。
回答by fwlx
A typical best practice is not using long/int/short directly. Instead, according to specification of compilers and OS, wrap them into a header file to ensure they hold exactly the amount of bits that you want. Then use int8/int16/int32 instead of long/int/short. For example, on 32bit Linux, you could define a header like this
典型的最佳实践是不直接使用 long/int/short。相反,根据编译器和操作系统的规范,将它们包装到一个头文件中,以确保它们恰好包含您想要的位数。然后使用 int8/int16/int32 而不是 long/int/short。例如,在 32 位 Linux 上,您可以定义这样的标头
typedef char int8;
typedef short int16;
typedef int int32;
typedef unsigned int uint32;