C++ 中的“long”、“long long”、“long int”和“long long int”有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18971732/
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
What is the difference between "long", "long long", "long int", and "long long int" in C++?
提问by 1110101001
I am transitioning from Java to C++ and have some questions about the long
data type. In Java, to hold an integer greater than 232, you would simply write long x;
. However, in C++, it seems that long
is both a data type and a modifier.
我正在从 Java 过渡到 C++,并且对long
数据类型有一些疑问。在 Java 中,要保存大于 2 32的整数,您只需编写long x;
. 但是,在 C++ 中,它似乎long
既是数据类型又是修饰符。
There seems to be several ways to use long
:
似乎有几种使用方法long
:
long x;
long long x;
long int x;
long long int x;
Also, it seems there are things such as:
此外,似乎还有一些事情,例如:
long double x;
and so on.
等等。
What is the difference between all of these various data types, and do they all have the same purpose?
所有这些不同的数据类型之间有什么区别,它们都具有相同的目的吗?
采纳答案by paxdiablo
long
and long int
are identical. So are long long
and long long int
. In both cases, the int
is optional.
long
并且long int
是相同的。那么,long long
和long long int
。在这两种情况下,int
都是可选的。
As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long
is at leastas wide as long
.
至于两组之间的差异,C ++的每个标准的任务的最小范围,并且long long
是在至少一样宽long
。
The controlling parts of the standard (C++11, but this has been around for a long time) are, for one, 3.9.1 Fundamental types
, section 2 (a later section gives similar rules for the unsigned integral types):
标准的控制部分(C++11,但这已经存在很长时间了)是3.9.1 Fundamental types
,第 2 部分(后面的部分为无符号整数类型提供了类似的规则):
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list.
有五种标准的有符号整数类型:signed char、short int、int、long int 和 long long int。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。
There's also a table 9 in 7.1.6.2 Simple type specifiers
, which shows the "mappings" of the specifiers to actual types (showing that the int
is optional), a section of which is shown below:
中还有一个表 9 7.1.6.2 Simple type specifiers
,它显示了说明符到实际类型的“映射”(表明int
是可选的),其中的一部分如下所示:
Specifier(s) Type
------------- -------------
long long int long long int
long long long long int
long int long int
long long int
Note the distinction there between the specifier and the type. The specifier is how you tell the compiler what the type is but you can use different specifiers to end up at the same type.
请注意说明符和类型之间的区别。说明符是你告诉编译器类型是什么的方式,但你可以使用不同的说明符来结束相同的类型。
Hence long
on its own is neither a type nora modifier as your question posits, it's simply a specifier for the long int
type. Ditto for long long
being a specifier for the long long int
type.
因此long
,它本身既不是您的问题所假定的类型也不是修饰符,它只是long int
类型的说明符。同样long long
作为long long int
类型的说明符。
Although the C++ standard itself doesn't specify the minimum ranges of integral types, it does cite C99, in 1.2 Normative references
, as applying. Hence the minimal ranges as set out in C99 5.2.4.2.1 Sizes of integer types <limits.h>
are applicable.
尽管 C++ 标准本身没有指定整数类型的最小范围,但它确实引用了 C99, in 1.2 Normative references
,作为应用。因此,中列出的最小范围C99 5.2.4.2.1 Sizes of integer types <limits.h>
是适用的。
In terms of long double
, that's actually a floating point value rather than an integer. Similarly to the integral types, it's required to have at least as much precision as a double
and to provide a superset of values over that type (meaning at leastthose values, not necessarily morevalues).
就 而言long double
,这实际上是一个浮点值而不是整数。与整数类型类似,它需要至少具有与 a 一样多的精度,double
并提供该类型的值的超集(意味着至少是这些值,不一定是更多的值)。
回答by Eric Lippert
Long and long int are at least 32 bits.
long 和 long int 至少为 32 位。
long long and long long int are at least 64 bits. You must be using a c99 compiler or better.
long long 和 long long int 至少为 64 位。您必须使用 c99 或更好的编译器。
long doubles are a bit odd. Look them up on Wikipedia for details.
长双打有点奇怪。有关详细信息,请在 Wikipedia 上查找它们。
回答by Brett Hale
long
is equivalent to long int
, just as short
is equivalent to short int
. A long int
is a signed integral type that is at least32 bits, while a long long
or long long int
is a signed integral type is at least64 bits.
long
等价于long int
,就像short
等价于 一样short int
。Along int
是至少32 位的带符号整数类型,而 a long long
orlong long int
是至少64 位的带符号整数类型。
This doesn't necessarily mean that a long long
is wider than a long
. Many platforms / ABIs use the LP64
model - where long
(and pointers) are 64 bits wide. Win64 uses the LLP64
, where long
is still 32 bits, and long long
(and pointers) are 64 bits wide.
这并不一定意味着 along long
比 a 宽long
。许多平台/ABI 使用该LP64
模型 - 其中long
(和指针)为 64 位宽。Win64 使用LLP64
,其中long
仍然是 32 位,并且long long
(和指针)是 64 位宽。
There's a good summary of 64-bit data models here.
有64位数据模型一个很好的总结在这里。
long double
doesn't guarantee much other than it will be at leastas wide as a double
.
long double
除了至少与double
.
回答by Siraj Alam
This looks confusing because you are taking long
as a datatype itself.
这看起来令人困惑,因为您将其long
视为数据类型本身。
long
is nothing but just the shorthand for long int
when you are using it alone.
long
只是long int
您单独使用它时的简写。
long
is a modifier, you can use it with double
also as long double
.
long
是一个修饰符,您也可以将它与double
as一起使用long double
。
long
== long int
.
long
= = long int
。
Both of them take 4 bytes.
它们都占用 4 个字节。
回答by thomiel
Historically, in early C times, when processors had 8 or 16 bit wordlength,int
was identical to todays short
(16 bit). In a certain sense, int is a more abstract data type thanchar
,short
,long
orlong long
, as you cannot be sure about the bitwidth.
从历史上看,在早期的 C 时代,当处理器具有 8 位或 16 位字长时,int
与今天的short
(16 位)相同。在一定意义上说,int是比一个更抽象的数据类型char
,short
,long
或者long long
,因为你不能肯定位宽。
When definingint n;
you could translate this with "give me the best compromise of bitwidth and speed on this machine for n". Maybe in the future you should expect compilers to translateint
to be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlong
as data type.
在定义时,int n;
您可以将其翻译为“在这台机器上为 n 提供位宽和速度的最佳折衷方案”。也许将来您应该期望编译器将其转换int
为 64 位。因此,当您希望变量具有 32 位而不是更多时,最好使用显式long
作为数据类型。
[Edit: #include <stdint.h>
seems to be the proper way to ensure bitwidths using the int##_t types, though it's not yet part of the standard.]
[编辑:#include <stdint.h>
似乎是使用 int##_t 类型确保位宽的正确方法,尽管它还不是标准的一部分。]