C语言 c/c++ 中的 unsigned long/long/int 有什么区别?

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

What's the difference between unsigned long/long/int in c/c++?

csyntaxintegerlong-integer

提问by user198729

It seems all of them take 4 bytes of space,

似乎它们都占用了 4 个字节的空间,

so what's the difference?

那么有什么区别呢?

回答by rlbond

First of all, the size of int/long is unspecified. So on your compiler, an intand a longmight be the same, but this isn't universal across compilers.

首先,int/long 的大小是未指定的。因此,在您的编译器上, anint和 along可能相同,但这在编译器之间并不通用。

As for the difference between unsigned longand long:

至于之间的区别unsigned longlong

Assuming 4 bytes, a longhas the range of -2,147,483,648to 2,147,483,647. An unsigned long has the range of 0to 4,294,967,295.

假设 4 个字节, along的范围为-2,147,483,648to 2,147,483,647。unsigned long 的范围为0to 4,294,967,295

One other difference is with overflow. For a signed type, an overflow has unspecified behavior. But for an unsigned type, overflow is guaranteed to "wrap around."

另一个区别是溢出。对于有符号类型,溢出具有未指定的行为。但是对于无符号类型,溢出保证会“环绕”。

回答by dthorpe

The C language specification allows the implementation of int and long types to vary from one platform to another within a few constraints. This variability is a headache for cross-platform code, but it is also an asset because it enables the informed programmer to balance their design goals between native processor speed and full numeric range on hardware architectures that don't offer both.

C 语言规范允许 int 和 long 类型的实现在一些约束内从一个平台到另一个平台不同。这种可变性对于跨平台代码来说是一个令人头疼的问题,但它也是一种资产,因为它使有见识的程序员能够在不提供两者的硬件架构上平衡本地处理器速度和全数字范围之间的设计目标。

In general, "int" is supposed to map a machine register size of the target CPU architecture's machine, so that loading, storing, and operating on the int type data should translate directly into operations that use the target processor's native registers.

一般来说,“int”应该映射目标 CPU 架构机器的机器寄存器大小,以便加载、存储和操作 int 类型数据应该直接转换为使用目标处理器的本地寄存器的操作。

Int can be less than the machine register size in the interest of saving memory space (big ints take up twice as much RAM as little ints). It's common to see int as a 32 bit entity even on 64 bit architectures where compatibility with older systems and memory efficiency are high priorities.

为了节省内存空间,int 可以小于机器寄存器大小(大 int 占用的 RAM 是小 int 的两倍)。即使在与旧系统的兼容性和内存效率是高优先级的 64 位体系结构上,也很常见将 int 视为 32 位实体。

"long" can be the same size or larger than "int" depending on the target architecture's register sizes. Operations on "long" may be implemented in software if the target architecture doesn't support values that large in its native machine registers.

“long”可以与“int”相同或更大,具体取决于目标架构的寄存器大小。如果目标架构不支持其本地机器寄存器中的大值,则可以在软件中实现对“long”的操作。

CPU chips designed for power efficiency or embedded devices are where you will find distinctions between int and long these days. Compilers for general purpose CPUs like in your desktop or laptop PC generally treat int and long as the same size because the CPU efficiently uses 32 bit registers. On smaller devices such as cell phones the CPU may be built to handle 16 bit data more naturally and have to work hard to handle 32 bit or larger data.

现在,您会发现 int 和 long 之间的区别是为提高能效或嵌入式设备而设计的 CPU 芯片。用于台式机或笔记本电脑等通用 CPU 的编译器通常将 int 和 long 视为相同大小,因为 CPU 有效地使用 32 位寄存器。在手机等较小的设备上,CPU 可能被构建为更自然地处理 16 位数据,并且必须努力处理 32 位或更大的数据。

Fewer bits per register means fewer circuits required on the chip, fewer data lines to move data in and out of the chip, lower power consumption and smaller chip die size, all of which make for a lower cost (in $ and in watts) device.

每个寄存器的位数更少意味着芯片上所需的电路更少,将数据移入和移出芯片的数据线更少,功耗更低,芯片尺寸更小,所有这些都有助于降低设备成本(以美元和瓦特计) .

In such an architecture, you will most likely find int to be 16 bits in size and long to be 32 bits in size. There may also be a performance penalty associated with using longs, caused by either wait states to load the 32 bits in multiple reads across a 16 bit data bus, or caused by implementing long operations (addition, subtraction, etc) in software if the native hardware doesn't support such operations in hardware.

在这样的架构中,您很可能会发现 int 的大小为 16 位,而 long 的大小为 32 位。还可能存在与使用 longs 相关的性能损失,这是由于等待状态在 16 位数据总线上的多次读取中加载 32 位引起的,或者是由在软件中实现长操作(加法、减法等)引起的,如果本机硬件不支持硬件中的此类操作。

As a general rule, the only thing you can assume about ints and longs is that the range of int should always be less than or equal to long on any architecture. You should also assume that someday your code will be recompiled for a different architecture where whatever relationship you currently see between int and long no longer exists.

作为一般规则,关于 int 和 long 的唯一假设是,int 的范围在任何架构上都应始终小于或等于 long。您还应该假设有一天您的代码将针对不同的体系结构重新编译,在这种体系结构中,您当前在 int 和 long 之间看到的任何关系都不再存在。

This is why you should be careful to keep ints separate from longs even in everyday mundane coding. They may be completely assignment compatible today because their implementation details for your current hardware platform coincide, but that coincidence is not guaranteed across all platforms.

这就是为什么即使在日常平凡的编码中你也应该小心地将整数与长整数分开。它们今天可能完全兼容,因为它们在您当前的硬件平台上的实现细节是一致的,但不能保证在所有平台上都是一致的。

回答by Dan Story

Well, the difference between unsigned longand longis simple -- the upper bound. Signed longgoes from (on an average 32-bit system) about -2.1 billion (-2^31) to +2.1 billion (+2^31 - 1), while unsigned longgoes from 0 to 4.2 billion (2^32 - 1).

好了,之间的区别unsigned long,并long很简单-上界。Signedlong从(在平均 32 位系统上)大约 -21 亿 (-2^31) 到 +21 亿 (+2^31 - 1),而unsigned long从 0 到 42 亿 (2^32 - 1)。

It so happens that on many compilers and operating systems (including, apparently, yours), intis also a 32-bit value. But the C++ standard doesn't determine maximum widths for any of these types, only minimumwidths. On some systems, intis 16 bits. On some systems, longis 64 bits. A lot of it depends on the processor architecture being targeted, and what its base word size is.

碰巧在许多编译器和操作系统(显然包括您的)上,int也是 32 位值。但是 C++ 标准没有确定任何这些类型的最大宽度,只有最小宽度。在某些系统上,int是 16 位。在某些系统上,long是 64 位。在很大程度上取决于目标处理器架构,以及它的基本字长是多少。

The header limits.hexists to define the maximum capacity of the various types under the current compilation environment, and stdint.hexists to provide environment-independent types of guaranteed width, such as int32_t.

header 的limits.h存在是为了定义当前编译环境下各种类型的最大容量,而stdint.h存在是为了提供保证宽度的环境无关类型,例如int32_t.