long 和 int 数据类型之间的区别

发布于 2024-07-21 18:54:52 字数 128 浏览 9 评论 0原文

考虑到以下语句返回4,C++中intlong类型有什么区别?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

旧城烟雨 2024-07-28 18:54:52

来自参考:

int 最初的目的是
“自然”字长
处理器。 许多现代处理器可以
用 equal 处理不同的字长
轻松。

另外,这一点:

在许多(但不是全部)C 和 C++ 上
实现中,a long 大于
一个整数。 当今最流行的桌面
平台,例如 Windows 和 Linux,
主要运行在 32 位处理器上
这些平台的大多数编译器都使用
具有相同大小的 32 位 int
并表示为 long。

From this reference:

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.

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.

时光匆匆的小流年 2024-07-28 18:54:52

标准给你的保证是这样的:

1 == sizeof(char) <= sizeof(short) <= sizeof (int) <= sizeof(long) <= sizeof(long long)

所以它对于 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:

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.

(Note that long long is recognized in C from C99 onwards, but was normally implemented as an extension in C++ prior to C++11.)

毁梦 2024-07-28 18:54:52

您使用的是 32 位计算机或 64 位 Windows 计算机。 在我的 64 位机器上(运行 Unix 衍生操作系统,而不是 Windows),sizeof(int) == 4,但是 sizeof(long) == 8

它们的类型不同——有时大小相同,有时大小不同。

(在过去,sizeof(int) == 2sizeof(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, but 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.)

友谊不毕业 2024-07-28 18:54:52

典型的最佳实践是不直接使用 long/int/short。 相反,根据编译器和操作系统的规范,将它们包装到头文件中,以确保它们准确地保存您想要的位数。 然后使用int8/int16/int32代替long/int/short。 例如,在 32 位 Linux 上,您可以定义如下标头

typedef char int8;
typedef short int16;
typedef int int32;
typedef unsigned int uint32;

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

typedef char int8;
typedef short int16;
typedef int int32;
typedef unsigned int uint32;
白龙吟 2024-07-28 18:54:52

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文