简体   繁体   English

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

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

As I've learned recently, a long in C/C++ is the same length as an int .正如我最近了解到,一个long在C / C ++是相同的长度为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?它是否具有int没有的特定用途? I know we can declare a 64-bit int like so:我知道我们可以像这样声明一个 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 ?但是为什么语言选择这样做,而不是仅仅制作一个long井...比int Other languages such as C# do this, so why not C/C++?其他语言如 C# 可以这样做,那么为什么 C/C++ 不可以呢?

When writing in C or C++, every datatype is architecture and compiler specific.用 C 或 C++ 编写时,每种数据类型都是特定于体系结构和编译器的。 On one system int is 32, but you can find ones where it is 16 or 64;在一个系统上 int 是 32,但你可以找到 16 或 64 的; it's not defined, so it's up to compiler.它没有定义,所以这取决于编译器。

As for long and int , it comes from times, where standard integer was 16bit, where long was 32 bit integer - and it indeed was longer than int .至于longint ,它来自次,其中标准的整数为16位,其中long为32位整数-这的确长于int

The specific guarantees are as follows:具体保证如下:

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

Thus it makes sense to use long if you need a type that's at least 32 bits, int if you need a type that's reasonably fast and at least 16 bits.因此,如果您需要至少 32 位的类型,则使用long是有意义的,如果您需要相当快且至少 16 位的类型,则使用int是有意义的。

Actually, at least in C, these lower bounds are expressed in terms of ranges , not sizes.实际上,至少在 C 中,这些下限是根据范围而不是大小来表示的。 For example, the language requires that INT_MIN <= -32767 , and INT_MAX >= +32767 .例如,该语言要求INT_MIN <= -32767INT_MAX >= +32767 The 16-bit requirements follows from this and from the requirement that integers are represented in binary. 16 位要求由此而来以及整数以二进制表示的要求。

C99 adds <stdint.h> and <inttypes.h> , which define types such as uint32_t , int_least32_t , and int_fast16_t ; C99 添加了<stdint.h><inttypes.h> ,它们定义了uint32_tint_least32_tint_fast16_t these are typedefs, usually defined as aliases for the predefined types.这些是 typedef,通常定义为预定义类型的别名。

(There isn't necessarily a direct relationship between size and range. An implementation could make int 32 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 int could be larger than long , as long as long has at least as wide a range as 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.) (大小和范围之间不一定有直接关系。实现可以使int 32 位,但范围仅为-2**23 .. +2^23-1 ,其他 8 位(称为填充位)对值没有贡献。理论上有可能(但实际上极不可能) int可能大于long ,只要long范围至少与int一样宽。实际上,很少有现代系统使用填充位,甚至是 2 的补码以外的表示,但标准仍然允许这种奇怪的情况。您更有可能在嵌入式系统中遇到奇特的功能。)

long is not the same length as an int. long 与 int 的长度不同。 According to the specification, long is at least as large as int.根据规范,long 至少与 int 一样大。 For example, on Linux x86_64 with GCC, sizeof(long) = 8, and sizeof(int) = 4.例如,在带有 GCC 的 Linux x86_64 上,sizeof(long) = 8 和 sizeof(int) = 4。

long is not the same size as int , it is at least the same size as int . longint大小不同,它至少int大小相同。 To quote the C++03 standard (3.9.1-2):引用 C++03 标准 (3.9.1-2):

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.”有四种有符号整数类型:“signed char”、“short int”、“int”和“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.提供其他有符号整数类型以满足特殊需要。

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".我对此的解释是“只需使用int ,但如果由于某种原因不符合您的需求,并且您很幸运找到了另一种更适合的整数类型,请成为我们的客人并使用该类型代替”。 One way that long might be better is if you 're on an architecture where it is... longer. long可能更好的一种方法是,如果您使用的是……更长的架构。

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. long 的大小是您的处理器一次可以操作的位数的大小。 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 更大的是“quadword”,其大小是 long long 的两倍但它没有正式名称(也不是真正的标准名称)。

Now, where does the int come in?现在, int 从哪里进来? 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.您的寄存器定义了 CPU 处理的本机大小,而这些大小又定义了短和长等内容的大小。 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。

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.在今天的 64 位机器上,您会假设,由于 long 是一个字,而 64 位机器上的一个字是 64 位,那么无论处理器设计用于处理什么,long 都是 64 位和 int,但它可能不是。 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.最终,如果您使用的是 Windows(并使用 Win64),则 long 和 int 都是 32 位。 Solaris and Linux use different definitions (the long is 64bits). Solaris 和 Linux 使用不同的定义(长为 64 位)。 These definitions are called things like ILP64, LP64, and LLP64.这些定义被称为 ILP64、LP64 和 LLP64 之类的东西。 Windows uses LLP64 and Solaris and Linux use LP64: 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, eg, 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 等类型设置整数的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM