在c++中“long”、“long long long long long long long int”、“long long long long long long int”的区别是什么?

时间:2022-05-23 16:50:57

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++,对长数据类型有一些疑问。在Java中,要保持一个大于232的整数,只需写长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?

所有这些不同的数据类型之间的区别是什么,它们都有相同的目的吗?

5 个解决方案

#1


125  

long and long int are identical. So are long long and long long int. In both cases, the int is optional.

长整数和长整数是相同的。长和长int都是可选的。

As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long is at least as wide as long.

至于两组之间的差异,c++标准规定了每一组的最小值范围,而且长时间至少是长。

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基本类型,第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.

有五个标准的带符号整数类型:带符号字符、短int、长int和长long 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:

在7.1.6.2简单类型说明符中也有一个表9,它显示了指定符对实际类型的“映射”(显示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 nor a 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本身既不是类型也不是修饰语,正如您的问题所提出的,它只是长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++标准本身没有指定积分类型的最小范围,但是它引用了1.2个规范引用中的C99作为应用。因此,C99 5.2.4.2.1中规定的最小范围为整数类型 适用。 。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 least those values, not necessarily more values).

对于长double,它实际上是一个浮点值而不是一个整数。与积分类型类似,它要求具有至少与双精度值相同的精度,并在该类型上提供值的超集(意味着至少要有这些值,不一定要有更多值)。

#2


42  

Long and long int are at least 32 bits.

长整数和长整数至少是32位。

long long and long long int are at least 64 bits. You must be using a c99 compiler or better.

长长和长整数至少是64位。您必须使用c99编译器或更好的。

long doubles are a bit odd. Look them up on Wikipedia for details.

长双打有点奇怪。在*上查找细节。

#3


12  

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 least 32 bits, while a long long or long long int is a signed integral type is at least 64 bits.

长整数等于长整数,就像短整数等于短整数一样。长整数是有符号整数类型,至少32位,而长整数或长整数是有符号整数类型,至少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.

这并不一定意味着长时间的长。许多平台/ ABIs使用LP64模型——其中长(和指针)是64位宽。Win64使用LLP64, long仍然是32位,长(和指针)是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 least as wide as a double.

长双折并不能保证什么,除非它至少和双折一样宽。

#4


7  

This looks confusing because you are taking long as a datatype itself.

这看起来很让人困惑,因为您使用的数据类型本身很长。

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 == long int.

长= = int。

Both of them take 4 bytes.

它们都需要4个字节。

#5


3  

Historically, in early C times, when processors had 8 or 16 bit wordlength,intwas identical to todays short(16 bit). In a certain sense, int is a more abstract data type thanchar,short,longorlong long, as you cannot be sure about the bitwidth.

在历史上,在早期的C时代,当处理器有8或16位字长时,与今天的短(16位)是一样的。在某种意义上,int是比char、short、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 translateintto be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlongas data type.

当定义int n时,你可以这样翻译:“给我在这台机器上对n的位宽和速度的最佳折衷”。也许在将来,您应该期望编译器可以翻译为64位。所以当你想让你的变量有32位而不是更多的时候,最好使用一个明确的longas数据类型。

[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类型确保位宽的正确方法,尽管它还不是标准的一部分。

#1


125  

long and long int are identical. So are long long and long long int. In both cases, the int is optional.

长整数和长整数是相同的。长和长int都是可选的。

As to the difference between the two sets, the C++ standard mandates minimum ranges for each, and that long long is at least as wide as long.

至于两组之间的差异,c++标准规定了每一组的最小值范围,而且长时间至少是长。

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基本类型,第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.

有五个标准的带符号整数类型:带符号字符、短int、长int和长long 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:

在7.1.6.2简单类型说明符中也有一个表9,它显示了指定符对实际类型的“映射”(显示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 nor a 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本身既不是类型也不是修饰语,正如您的问题所提出的,它只是长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++标准本身没有指定积分类型的最小范围,但是它引用了1.2个规范引用中的C99作为应用。因此,C99 5.2.4.2.1中规定的最小范围为整数类型 适用。 。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 least those values, not necessarily more values).

对于长double,它实际上是一个浮点值而不是一个整数。与积分类型类似,它要求具有至少与双精度值相同的精度,并在该类型上提供值的超集(意味着至少要有这些值,不一定要有更多值)。

#2


42  

Long and long int are at least 32 bits.

长整数和长整数至少是32位。

long long and long long int are at least 64 bits. You must be using a c99 compiler or better.

长长和长整数至少是64位。您必须使用c99编译器或更好的。

long doubles are a bit odd. Look them up on Wikipedia for details.

长双打有点奇怪。在*上查找细节。

#3


12  

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 least 32 bits, while a long long or long long int is a signed integral type is at least 64 bits.

长整数等于长整数,就像短整数等于短整数一样。长整数是有符号整数类型,至少32位,而长整数或长整数是有符号整数类型,至少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.

这并不一定意味着长时间的长。许多平台/ ABIs使用LP64模型——其中长(和指针)是64位宽。Win64使用LLP64, long仍然是32位,长(和指针)是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 least as wide as a double.

长双折并不能保证什么,除非它至少和双折一样宽。

#4


7  

This looks confusing because you are taking long as a datatype itself.

这看起来很让人困惑,因为您使用的数据类型本身很长。

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 == long int.

长= = int。

Both of them take 4 bytes.

它们都需要4个字节。

#5


3  

Historically, in early C times, when processors had 8 or 16 bit wordlength,intwas identical to todays short(16 bit). In a certain sense, int is a more abstract data type thanchar,short,longorlong long, as you cannot be sure about the bitwidth.

在历史上,在早期的C时代,当处理器有8或16位字长时,与今天的短(16位)是一样的。在某种意义上,int是比char、short、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 translateintto be 64 bit. So when you want your variable to have 32 bits and not more, better use an explicitlongas data type.

当定义int n时,你可以这样翻译:“给我在这台机器上对n的位宽和速度的最佳折衷”。也许在将来,您应该期望编译器可以翻译为64位。所以当你想让你的变量有32位而不是更多的时候,最好使用一个明确的longas数据类型。

[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类型确保位宽的正确方法,尽管它还不是标准的一部分。