“long”,“long long”,“long int”和“long long int”之间的区别在于,在C ++? [英] What is the difference between "long", "long long", "long int", and "long long int" in C++?
问题描述
我从Java转换到C ++,并对 long
数据类型有一些疑问。在Java中,要保存大于2 32 的整数,只需写 long x;
即可。但是,在C ++中,看来 long
是数据类型和修饰符。
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.
似乎有几种方法可以使用 long
:
There seems to be several ways to use long
:
long x;
long long x;
long int x;
long long int x;
此外,似乎还有一些事情:
Also, it seems there are things such as:
long double x;
等等。
所有这些不同的数据类型之间的区别,它们都有相同的目的?
What is the difference between all of these various data types, and do they all have the same purpose?
推荐答案
long
和 long int
是相同的。因此 long long
和 long long int
。在这两种情况下, int
是可选的。
long
and long int
are identical. So are long long
and long long int
. In both cases, the int
is optional.
对于两个集合之间的区别,C ++标准每个的最小范围,并且 long long
以 long
的宽度至少。
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 ++ 11,但这已经存在了很长时间了),例如, 3.9。 1基本类型
,第2节(稍后部分为无符号整数类型提供类似的规则):
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):
是五个标准的有符号整数类型:signed char,short int,int,long int和long long int。在此列表中,每种类型至少提供与列表中之前的存储器相同的存储器。
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.
code> 7.1.6.2简单类型说明符,它显示了说明符与实际类型的映射(显示 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:
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.
因此 long
虽然C ++标准本身没有指定整数类型的最小范围,但它引用了C99,在 1.2规范引用
中。因此, C99中规定的最小范围5.2.4.2.1整数类型< limits.h>
的大小适用。
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.
在 long double
方面,实际上是一个浮点值而不是一个整数。类似于整数类型,它需要具有至少与 double
一样多的精度,并且提供超过该类型的值的超集(意味着至少这些值,不一定是更多值)。
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).
这篇关于“long”,“long long”,“long int”和“long long int”之间的区别在于,在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!