长整数的表示

发布于 2024-10-18 08:12:34 字数 520 浏览 6 评论 0原文

可能的重复:
int 和 int 之间有什么区别C++ 中的 long?

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烦人精 2024-10-25 08:12:34

您拥有的保证是:

sizeof(int) <= sizeof(long)

sizeof(int)   * CHAR_BITS >= 16
sizeof(long)  * CHAR_BITS >= 32
CHAR_BITS                 >= 8

满足所有这些条件:

sizeof(int)  == 4
sizeof(long) == 4

The guarantees you have are:

sizeof(int) <= sizeof(long)

sizeof(int)   * CHAR_BITS >= 16
sizeof(long)  * CHAR_BITS >= 32
CHAR_BITS                 >= 8

All these conditions are met with:

sizeof(int)  == 4
sizeof(long) == 4
你如我软肋 2024-10-25 08:12:34

C++ 语言从不保证或要求 long intint更大。该语言唯一有希望的是 long int 不小于 int。在许多流行的实现中,long int 的大小与 int 相同。

C++ langauge never guaranteed or required long int to be bigger than int. The only thing the language is promising is that long int is not smaller than int. In many popular implementations long int has the same size as int.

咿呀咿呀哟 2024-10-25 08:12:34

这取决于语言和平台。例如,在 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.

深海少女心 2024-10-25 08:12:34

否。根据 C 标准,intshort int 的大小相同,int的大小相同>long int,对于 int 来说,不能与 long intshort 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 as short int, for int to be the same size as long int, for int not to be the same as either long int or short int, or even for all three to be the same size. On 16-bit machines it was common for sizeof(int) == sizeof(short int) == 2 and sizeof(long int) == 4, but the most common arrangement on 32-bit machines is sizeof(int) == sizeof(long int) == 4 and sizeof(short int) == 2. And on 64-bit machines you may find sizeof(short int) == 2, sizeof(int) == 4, and sizeof(long int) == 8.

×眷恋的温暖 2024-10-25 08:12:34

请参阅http://bytes.com/topic/c/答案/163333-sizeof-int-sizeof-long-int

没有什么问题。数据类型的大小通常取决于硬件(char 除外,它始终为 1 个寄存器宽)。在gcc/Unix中,int和long int都需要4个字节。您还可以尝试 sizeof(long long int) 并查看结果和 sizeof(short int)。

See http://bytes.com/topic/c/answers/163333-sizeof-int-sizeof-long-int.

No nothing is wrong. The size of data-types is generally hardware dependant (exception being char which is always 1 register wide). In gcc/Unix, int and long int both require 4 bytes. You can also try sizeof(long long int) and see the results and sizeof(short int).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文