int和long在C ++中有什么区别?

纠正我,如果我错了,

int是4个字节,范围从-2,147,483,648到2,147,483,647(2 ^ 31)
长为4个字节,范围从-2,147,483,648到2,147,483,647(2 ^ 31)

C ++有什么区别? 它们可以互换使用吗?


它依赖于实现。

例如,在Windows下它们是相同的,但是例如在Alpha系统上,长度是64位,而int是32位。 本文介绍了可变平台上的英特尔C ++编译器规则。 总结:

  OS           arch           size
Windows       IA-32        4 bytes
Windows       Intel 64     4 bytes
Windows       IA-64        4 bytes
Linux         IA-32        4 bytes
Linux         Intel 64     8 bytes
Linux         IA-64        8 bytes
Mac OS X      IA-32        4 bytes
Mac OS X      Intel 64     8 bytes  

唯一的保证是:

sizeof(char) == 1
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

// FROM @KTC. The C++ standard also has:
sizeof(signed char)   == 1
sizeof(unsigned char) == 1

// NOTE: These size are not specified explicitly in the standard.
//       They are implied by the minimum/maximum values that MUST be supported
//       for the type. These limits are defined in limits.h
sizeof(short)     * CHAR_BIT >= 16
sizeof(int)       * CHAR_BIT >= 16
sizeof(long)      * CHAR_BIT >= 32
sizeof(long long) * CHAR_BIT >= 64
CHAR_BIT         >= 8   // Number of bits in a byte

另见: long保证至少32位?


编译x64时,int和long之间的差别在0到4个字节之间,具体取决于您使用的编译器。

GCC使用LP64模型,这意味着在64位模式下,整数是32位,但长度是64位。

例如MSVC使用LLP64模型,这意味着即使在64位模式下,int和long也是32位。

链接地址: http://www.djcxy.com/p/6893.html

上一篇: What is the difference between an int and a long in C++?

下一篇: Alternatives to gprof