long 和 int 数据类型之间的区别
考虑到以下语句返回4
,C++中int
和long
类型有什么区别?
sizeof(int)
sizeof(long)
Considering that the following statements return 4
, what is the difference between the int
and long
types in C++?
sizeof(int)
sizeof(long)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自此参考:
另外,这一点:
From this reference:
Also, this bit:
标准给你的保证是这样的:
所以它对于
sizeof (int)
和sizeof (long)
相等是完全有效的,并且许多平台选择这样做方法。 你会发现一些平台上的int
是32位,long
是64位,而long long
是128位,但这似乎很常见sizeof (long)
为 4。(请注意,
long long
从 C99 开始在 C 中被识别,但通常在 C++11 之前作为 C++ 的扩展实现.)The guarantees the standard gives you go like this:
So it's perfectly valid for
sizeof (int)
andsizeof (long)
to be equal, and many platforms choose to go with this approach. You will find some platforms whereint
is 32 bits,long
is 64 bits, andlong long
is 128 bits, but it seems very common forsizeof (long)
to be 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.)您使用的是 32 位计算机或 64 位 Windows 计算机。 在我的 64 位机器上(运行 Unix 衍生操作系统,而不是 Windows),
sizeof(int) == 4
,但是sizeof(long) == 8
。它们的类型不同——有时大小相同,有时大小不同。
(在过去,
sizeof(int) == 2
和sizeof(long) == 4
— 尽管那可能是 C++ 出现之前的日子,但现在看来想想看,从技术上讲,它是一个合法的配置,尽管在嵌入式空间之外很不寻常,甚至在嵌入式空间中也很可能不寻常。)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
, butsizeof(long) == 8
.They're different types — sometimes the same size as each other, sometimes not.
(In the really old days,
sizeof(int) == 2
andsizeof(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.)典型的最佳实践是不直接使用 long/int/short。 相反,根据编译器和操作系统的规范,将它们包装到头文件中,以确保它们准确地保存您想要的位数。 然后使用int8/int16/int32代替long/int/short。 例如,在 32 位 Linux 上,您可以定义如下标头
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 大小相同,并且可能但不一定更长。
在常见的 32 位系统上,int 和 long 都是 4 字节/32 位,根据 C++ 规范,这是有效的。
在其他系统上,int 和 long long 的大小可能不同。 我曾经在一个平台上工作,其中 int 是 2 字节,long 是 4 字节。
The long must be at least the same size as an int, and possibly, but not necessarily, longer.
On common 32-bit systems, both int and long are 4-bytes/32-bits, and this is valid according to the C++ spec.
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.
在它们都具有相同大小的平台上,答案是什么。 它们都表示带符号的 4 字节值。
然而你不能相信这是真的。 标准没有明确定义 long 和 int 的大小。 编译器可以给类型不同的大小,从而打破这个假设。
On platforms where they both have the same size the answer is nothing. They both represent signed 4 byte values.
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.