C++ “长”数据类型用于什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4329277/
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's the 'long' data type used for?
提问by Alex
I've been programming in C++ for quite a while now and I am pretty familiar with most of the stuff. One thing that I've never understood though is the 'long' data type.
我用 C++ 编程已经有一段时间了,我对大多数东西都非常熟悉。我从未理解的一件事是“长”数据类型。
I googled it but I still don't know what it is for. I've found pages that say it is the same size and has the same range as an int. So what would be the point in using it?
我用谷歌搜索了它,但我仍然不知道它是做什么用的。我发现页面说它与 int 具有相同的大小和相同的范围。那么使用它有什么意义呢?
I found another stack overflow question regarding this here: Difference between long and int data types
我在这里发现了另一个堆栈溢出问题: Difference between long and int data types
And it seems that the only difference between the two is that sometimes the size is different on different systems. Does that mean that an application that uses long on a 64bit machine won't work on a 32bit machine? If so then wouldn't it be better to not use them at all?
而且似乎两者之间的唯一区别是有时在不同系统上的大小不同。这是否意味着在 64 位机器上使用 long 的应用程序不能在 32 位机器上运行?如果是这样,那么根本不使用它们不是更好吗?
Also I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
我还注意到名为“long int”甚至“long long”的东西!它是数据类型还是修饰符?
回答by slebetman
It is compiler dependent. My standards-fu is a bit rusty but I believe it is defined as follows:
它依赖于编译器。我的标准 fu 有点生疏,但我相信它的定义如下:
char <= short <= int <= long <= long long
where:
在哪里:
char >= 8 bits
short >= 16 bits
int >= 16 bits
long >= 32 bits
long long >= 64 bits
Which means that it is perfectly valid to have char = short = int = long = long long = 64bits
and in fact compilers of some DSPs are designed that way.
这意味着拥有char = short = int = long = long long = 64bits
并且实际上某些 DSP 的编译器就是这样设计的,这是完全有效的。
This underscores the importance of actually reading your compiler documentation.
这强调了实际阅读编译器文档的重要性。
回答by fredoverflow
I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
我注意到名为“long int”甚至“long long”的东西!它是数据类型还是修饰符?
long int
is the same as long
(just as short int
is the same as short
).
long int
是一样的long
(就像short int
是一样short
)。
long long
is a distinct data type introduced by several compilers and adopted by C++0x.
long long
是由多个编译器引入并被 C++0x 采用的独特数据类型。
Note that there is no such thing as long long long
:
请注意,没有这样的事情long long long
:
error: 'long long long' is too long for GCC
error: 'long long long' is too long for GCC
回答by Nate
From one of the answers in the question you linked:
从您链接的问题中的答案之一:
The long must be at least the same size as an int, and possibly, but not necessarily, longer.
long 必须至少与 int 大小相同,并且可能(但不一定)更长。
I can't think of a better way to explain it.
我想不出更好的方法来解释它。
回答by Cheers and hth. - Alf
long
is guaranteed (at least) 32 bits. int
is only guaranteed (at least) 16 bits. on 16- and 8-bit systems long
provided range at a cost of efficiency.
long
保证(至少)32 位。int
仅保证(至少)16 位。在 16 位和 8 位系统long
上以牺牲效率为代价提供范围。
cheers & hth.,
干杯 & hth.,
回答by icecrime
This is what the C++03 standard says (3.9.1/2) :
这是 C++03 标准所说的 (3.9.1/2):
There are foursigned 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.
有四种有符号整数类型:“signed char”、“short int”、“int”和“long int”。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。
So : sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
所以 : sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
This is what the C++0x (3.9.1/2) and C99 (6.2.5/4) standards say :
这就是 C++0x (3.9.1/2) 和 C99 (6.2.5/4) 标准所说的:
There are fivestandard signed integer types, designated as signed char, short int, int, long int, and long long int.
有五种标准的有符号整数类型,分别指定为有符号 char、short int、int、long int 和 long long int。
long
is synonym oflong int
long long
doesn't exist in C++03, but will in C++0x.
long
是同义词long int
long long
在 C++03 中不存在,但在 C++0x 中存在。
回答by pythonic metaphor
If you want an integer that is guaranteed to be 32 bit or 64 bit, there are such types, e.g. int64_t. If you really want to ensure your int are of such a size, use these types instead of long, long long, etc. You'll need to include cstdint
for these (stdint.h in C).
如果你想要一个保证是 32 位或 64 位的整数,有这样的类型,例如 int64_t。如果您真的想确保您的 int 具有这样的大小,请使用这些类型而不是 long、long long 等。您需要包含cstdint
这些类型(C 中的 stdint.h)。
回答by dan04
I googled it but I still don't know what its for. I've found pages that say its the same size and has the same range as an int. So what would be the point in using it?
我用谷歌搜索了它,但我仍然不知道它的用途。我发现页面的大小和范围与 int 相同。那么使用它有什么意义呢?
I've wondered the same thing.And concluded that long
is now useless.
我也想过同样的事情。并得出结论,long
现在是无用的。
Prior to the rise of 64-bit systems, the de factostandard for C integer types was:
在 64 位系统兴起之前,C 整数类型的实际标准是:
char
= (u
)int8_t
(Note that C predates Unicode.)short
=int16_t
int
=intptr_t
[until 64-bit],int_fast16_t
long
=int32_t
[until 64-bit],intmax_t
[until 1999]long long
=int64_t
orintmax_t
char
= (u
)int8_t
(注意 C 早于 Unicode。)short
=int16_t
int
=intptr_t
[直到 64 位],int_fast16_t
long
=int32_t
[直到 64 位],intmax_t
[直到 1999]long long
=int64_t
或intmax_t
Today, however, long
has no consistent semantics.
然而,今天long
没有一致的语义。