长对。Int C/C++ - 重点是什么?

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

Long Vs. Int C/C++ - What's The Point?

c++cintlong-integer

提问by MGZero

As I've learned recently, a long in C/C++ is the same length as an int. To put it simply, why? It seems almost pointless to even include the datatype in the language. Does it have any uses specific to it that an int doesn't have? I know we can declare a 64-bit int like so:

正如我最近了解到的,C/C++ 中的 long 与 int 的长度相同。简单的说,为什么?甚至在语言中包含数据类型似乎几乎毫无意义。它是否具有 int 没有的特定用途?我知道我们可以像这样声明一个 64 位 int:

long long x = 0;

But why does the language choose to do it this way, rather than just making a long well...longer than an int? Other languages such as C# do this, so why not C/C++?

但是为什么语言选择这样做,而不是仅仅制作一个长井......比int更长?其他语言如 C# 可以这样做,那么为什么 C/C++ 不可以呢?

回答by Griwes

When writing in C or C++, every datatype is architecture and compiler specific. On one system int is 32, but you can find ones where it is 16 or 64; it's not defined, so it's up to compiler.

用 C 或 C++ 编写时,每种数据类型都是特定于体系结构和编译器的。在一个系统上 int 是 32,但你可以找到 16 或 64 的;它没有定义,所以这取决于编译器。

As for longand int, it comes from times, where standard integer was 16bit, where longwas 32 bit integer - and it indeed waslonger than int.

至于longint,它来自次,其中标准的整数为16位,其中long为32位整数-这的确长于int

回答by Keith Thompson

The specific guarantees are as follows:

具体保证如下:

  • charis at least 8 bits (1 byte by definition, however many bits it is)
  • shortis at least 16 bits
  • intis at least 16 bits
  • longis at least 32 bits
  • long long(in versions of the language that support it) is at least 64 bits
  • Each type in the above list is at leastas wide as the previous type (but may well be the same).
  • char至少为 8 位(根据定义为 1 个字节,无论它是多少位)
  • short至少是 16 位
  • int至少是 16 位
  • long至少是 32 位
  • long long(在支持它的语言版本中)至少是 64 位
  • 上面列表中的每种类型至少与前一种类型一样宽(但很可能相同)。

Thus it makes sense to use longif you need a type that's at least 32 bits, intif you need a type that's reasonably fast and at least 16 bits.

因此,long如果您需要一个至少 32 位int的类型,如果您需要一个相当快且至少 16 位的类型,那么使用它是有意义的。

Actually, at least in C, these lower bounds are expressed in terms of ranges, not sizes. For example, the language requires that INT_MIN <= -32767, and INT_MAX >= +32767. The 16-bit requirements follows from this and from the requirement that integers are represented in binary.

实际上,至少在 C 中,这些下限是用range而不是大小来表示的。例如,该语言要求INT_MIN <= -32767, 和INT_MAX >= +32767。16 位要求由此而来以及整数以二进制表示的要求。

C99 adds <stdint.h>and <inttypes.h>, which define types such as uint32_t, int_least32_t, and int_fast16_t; these are typedefs, usually defined as aliases for the predefined types.

C99添加<stdint.h><inttypes.h>,它定义类型,例如uint32_tint_least32_t,和int_fast16_t; 这些是 typedef,通常定义为预定义类型的别名。

(There isn't necessarilya direct relationship between size and range. An implementation could make int32 bits, but with a range of only, say, -2**23 .. +2^23-1, with the other 8 bits (called padding bits) not contributing to the value. It's theoretically possible (but practically highly unlikely) that intcould be larger than long, as long as longhas at least as wide a rangeas int. In practice, few modern systems use padding bits, or even representations other than 2's-complement, but the standard still permits such oddities. You're more likely to encounter exotic features in embedded systems.)

(大小和范围之间不一定有直接关系。一个实现可以产生int32 位,但范围只有,比如说,-2**23 .. +2^23-1其他 8 位(称为填充位)对值没有贡献。理论上是可能的(但实际上是极不可能的),其int可能是大于long,只要long有至少宽范围int。在实践中,一些现代的系统使用的填充比特,或大于二进制补码等甚至表示,但标准仍允许这样的怪事。您更有可能在嵌入式系统中遇到奇特的功能。)

回答by arsenm

long is not the same length as an int. According to the specification, long is at least as large as int. For example, on Linux x86_64 with GCC, sizeof(long) = 8, and sizeof(int) = 4.

long 与 int 的长度不同。根据规范,long 至少与 int 一样大。例如,在带有 GCC 的 Linux x86_64 上,sizeof(long) = 8 和 sizeof(int) = 4。

回答by Jon

longis not the same size as int, it is at leastthe same size as int. To quote the C++03 standard (3.9.1-2):

long与 的大小不同int,但至少与 的大小相同int。引用 C++03 标准 (3.9.1-2):

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment); the other signed integer types are provided to meet special needs.

有四种有符号整数类型:“signed char”、“short int”、“int”和“long int”。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。普通整数具有执行环境架构建议的自然大小);提供其他有符号整数类型以满足特殊需要。

My interpretation of this is "just use int, but if for some reason that doesn't fit your needs and you are lucky to find another integral type that's better suited, be our guest and use that one instead". One way that longmight be better is if you 're on an architecture where it is... longer.

我对此的解释是“仅使用int,但如果由于某种原因不符合您的需求,并且您很幸运找到了另一种更适合的整体类型,请成为我们的客人并改用该类型”。一种long可能更好的方法是,如果您使用的是……更长的架构。

回答by bueller

looking for something completely unrelated and stumbled across this and needed to answer. Yeah, this is old, so for people who surf on in later...

寻找完全不相关的东西,偶然发现了这个,需要回答。是的,这是旧的,所以对于以后上网的人......

Frankly, I think all the answers on here are incomplete.

坦率地说,我认为这里的所有答案都不完整。

The size of a long is the size of the number of bits your processor can operate on at one time. It's also called a "word". A "half-word" is a short. A "doubleword" is a long long and is twice as large as a long (and originally was only implemented by vendors and not standard), and even bigger than a long long is a "quadword" which is twice the size of a long long but it had no formal name (and not really standard).

long 的大小是您的处理器一次可以操作的位数的大小。它也被称为“词”。“半字”是短句。“双字”是 long long 是 long 的两倍(最初仅由供应商实现而不是标准),甚至比 long long 更大的是“quadword”,其大小是 long long 的两倍但它没有正式名称(也不是真正的标准名称)。

Now, where does the int come in? In part registers on your processor, and in part your OS. Your registers define the native sizes the CPU handles which in turn define the size of things like the short and long. Processors are also designed with a data size that is the most efficient size for it to operate on. That should be an int.

现在, int 从哪里进来?部分注册在您的处理器上,部分注册在您的操作系统上。您的寄存器定义了 CPU 处理的本机大小,而这些大小又定义了短和长等内容的大小。处理器还设计有数据大小,这是它操作的最有效大小。那应该是一个int。

On todays 64bit machines you'd assume, since a long is a word and a word on a 64bit machine is 64bits, that a long would be 64bits and an int whatever the processor is designed to handle, but it might not be. Why? Your OS has chosen a data model and defined these data sizes for you (pretty much by how it's built). Ultimately, if you're on Windows (and using Win64) it's 32bits for both a long and int. Solaris and Linux use different definitions (the long is 64bits). These definitions are called things like ILP64, LP64, and LLP64. Windows uses LLP64 and Solaris and Linux use LP64:

在今天的 64 位机器上,您会假设,由于 long 是一个字,而 64 位机器上的一个字是 64 位,那么无论处理器设计用于处理什么,long 都是 64 位和 int,但它可能不是。为什么?您的操作系统已经选择了一个数据模型并为您定义了这些数据大小(很大程度上取决于它的构建方式)。最终,如果您使用的是 Windows(并使用 Win64),则 long 和 int 都是 32 位。Solaris 和 Linux 使用不同的定义(长为 64 位)。这些定义被称为 ILP64、LP64 和 LLP64 之类的东西。Windows 使用 LLP64,Solaris 和 Linux 使用 LP64:

Model      ILP64   LP64   LLP64
int        64      32     32
long       64      64     32
pointer    64      64     64
long long  64      64     64

Where, e.g., ILP means int-long-pointer, and LLP means long-long-pointer

其中,例如,ILP 表示 int-long-pointer,而 LLP 表示 long-long-pointer

To get around this most compilers seem to support setting the size of an integer directly with types like int32 or int64.

为了解决这个问题,大多数编译器似乎支持直接使用 int32 或 int64 等类型设置整数的大小。