长整数的表示
#include <iostream>
int main()
{
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(long int) << std::endl;
}
输出:
4
4
这怎么可能? long int
的大小不应该比 int
更大吗?
Possible Duplicate:
What is the difference between an int and a long in C++?
#include <iostream>
int main()
{
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(long int) << std::endl;
}
Output:
4
4
How is this possible? Shouldn't long int
be bigger in size than int
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您拥有的保证是:
满足所有这些条件:
The guarantees you have are:
All these conditions are met with:
C++ 语言从不保证或要求
long int
比int
更大。该语言唯一有希望的是long int
不小于int
。在许多流行的实现中,long int
的大小与int
相同。C++ langauge never guaranteed or required
long int
to be bigger thanint
. The only thing the language is promising is thatlong int
is not smaller thanint
. In many popular implementationslong int
has the same size asint
.这取决于语言和平台。例如,在 ISO/ANSI C 中,长整数类型在 64 位系统 Unix 中为 8 字节,在其他操作系统/平台中为 4 字节。
It depends on the language and the platform. In ISO/ANSI C, For example, the long integer type is 8 bytes in 64-bit system Unix, and 4 bytes in other os/platforms.
否。根据 C 标准,
int
与short int
的大小相同,int
与的大小相同>long int
,对于int
来说,不能与long int
或short int
相同,或者甚至对于所有三个都不相同大小相同。在 16 位机器上,常见的是sizeof(int)
==sizeof(short int)
== 2 和sizeof(long int)
== 4,但 32 位机器上最常见的排列是sizeof(int)
==sizeof(long int)
== 4 和sizeof(short int)
== 2。在 64 位机器上,您可能会发现sizeof(short int)
== 2、sizeof(int)
== 4、和 sizeof(long int) == 8。No. It's valid under the C standard for
int
to be the same size asshort int
, forint
to be the same size aslong int
, forint
not to be the same as eitherlong int
orshort int
, or even for all three to be the same size. On 16-bit machines it was common forsizeof(int)
==sizeof(short int)
== 2 andsizeof(long int)
== 4, but the most common arrangement on 32-bit machines issizeof(int)
==sizeof(long int)
== 4 andsizeof(short int)
== 2. And on 64-bit machines you may findsizeof(short int)
== 2,sizeof(int)
== 4, andsizeof(long int)
== 8.请参阅http://bytes.com/topic/c/答案/163333-sizeof-int-sizeof-long-int。
See http://bytes.com/topic/c/answers/163333-sizeof-int-sizeof-long-int.