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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/271076/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:12:51  来源:igfitidea点击:

What is the difference between an int and a long in C++?

c++variables

提问by Joel

Correct me if I am wrong,

如果我错了,请纠正我

int is 4 bytes, with a range of values from -2,147,483,648 to 2,147,483,647 (2^31)
long is 4 bytes, with a range of values from -2,147,483,648 to 2,147,483,647 (2^31)

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

What is the difference in C++? Can they be used interchangeably?

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

采纳答案by Rob Walker

It is implementation dependent.

它依赖于实现。

For example, under Windows they are the same, but for example on Alpha systems a long was 64 bits whereas an int was 32 bits. This articlecovers the rules for the Intel C++ compiler on variable platforms. To summarize:

例如,在 Windows 下它们是相同的,但例如在 Alpha 系统上,long 是 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  

回答by Martin York

The only guarantee you have are:

您唯一的保证是:

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

Also see: Is longguaranteed to be at least 32 bits?

另请参阅:是否long保证至少为 32 位?

回答by Adrian

When compiling for x64, the difference between int and long is somewhere between 0 and 4 bytes, depending on what compiler you use.

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

GCC uses the LP64 model, which means that ints are 32-bits but longs are 64-bits under 64-bit mode.

GCC 使用 LP64 模型,这意味着在 64 位模式下 int 是 32 位,而 long 是 64 位。

MSVC for example uses the LLP64 model, which means both ints and longs are 32-bits even in 64-bit mode.

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

回答by Kevin Haines

The C++ specification itself(old version but good enough for this) leaves this open.

C ++规范本身(旧版本,但够用了这一点)离开这个开放。

There are four signed 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. Plain ints have the natural size suggested by the architecture of the execution environment* ;

[Footnote: that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>. --- end foonote]

有四种有符号整数类型:“ signed char”、“ short int”、“ int”和“ long int”。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。普通整数具有执行环境架构建议的自然大小*;

[脚注:即,足够大以包含 INT_MIN 和 INT_MAX 范围内的任何值,如标题中所定义<climits>。---结束脚注]

回答by Johannes Schaub - litb

As Kevin Haines points out, ints have the natural size suggested by the execution environment, which has to fit within INT_MIN and INT_MAX.

正如 Kevin Haines 指出的那样,int 具有执行环境建议的自然大小,它必须适合 INT_MIN 和 INT_MAX。

The C89 standard states that UINT_MAXshould be at least 2^16-1, USHRT_MAX2^16-1 and ULONG_MAX2^32-1 . That makes a bit-count of at least 16 for short and int, and 32 for long. For char it states explicitly that it should have at least 8 bits (CHAR_BIT). C++ inherits those rules for the limits.h file, so in C++ we have the same fundamental requirements for those values. You should however notderive from that that int is at least 2 byte. Theoretically, char, int and long could all be 1 byte, in which case CHAR_BITmust be at least 32. Just remember that "byte" is always the size of a char, so if char is bigger, a byte is not only 8 bits any more.

C89 标准规定UINT_MAX至少应为 2^16-1、2 USHRT_MAX^16-1 和ULONG_MAX2^32-1。这使得 short 和 int 的位计数至少为 16,而 long 为 32。对于 char 它明确指出它应该至少有 8 位 ( CHAR_BIT)。C++ 继承了limits.h 文件的这些规则,因此在C++ 中,我们对这些值有相同的基本要求。但是,您应该派生从这个int至少为2个字节。理论上,char、int 和 long 都可以是 1 个字节,在这种情况下CHAR_BIT必须至少为 32。请记住,“字节”始终是一个 char 的大小,因此如果 char 更大,一个字节不仅是 8 位任何更多的。

回答by Andru Luvisi

It depends on your compiler. You are guaranteed that a long will be at least as large as an int, but you are not guaranteed that it will be any longer.

这取决于您的编译器。您可以保证 long 至少与 int 一样大,但不能保证它会更长。

回答by Windows programmer

For the most part, the number of bytes and range of values is determined by the CPU's architecture not by C++. However, C++ sets minimum requirements, which litb explained properly and Martin York only made a few mistakes with.

在大多数情况下,字节数和值的范围由 CPU 的架构决定,而不是由 C++ 决定。然而,C++ 设置了最低要求,这点 litb 解释得很好,而 Martin York 只犯了一些错误。

The reason why you can't use int and long interchangeably is because they aren't always the same length. C was invented on a PDP-11 where a byte had 8 bits, int was two bytes and could be handled directly by hardware instructions. Since C programmers often needed four-byte arithmetic, long was invented and it was four bytes, handled by library functions. Other machines had different specifications. The C standard imposed some minimum requirements.

不能互换使用 int 和 long 的原因是因为它们的长度并不总是相同。C 是在 PDP-11 上发明的,其中一个字节有 8 位,int 是两个字节,可以直接由硬件指令处理。由于 C 程序员经常需要四字节算术,因此发明了 long 并且它是四字节,由库函数处理。其他机器有不同的规格。C 标准强加了一些最低要求。

回答by Roger Nelson

Relying on the compiler vendor's implementation of primitive type sizes WILL come back to haunt you if you ever compile your code on another machine architecture, OS, or another vendor's compiler .

如果您曾经在其他机器架构、操作系统或其他供应商的编译器上编译您的代码,那么依赖编译器供应商对原始类型大小的实现将再次困扰您。

Most compiler vendors provide a header file that defines primitive types with explict type sizes. These primitive types should be used when ever code may be potentially ported to another compiler (read this as ALWAYS in EVERY instance). For example, most UNIX compilers have int8_t uint8_t int16_t int32_t uint32_t. Microsoft has INT8 UINT8 INT16 UINT16 INT32 UINT32. I prefer Borland/CodeGear's int8 uint8 int16 uint16 int32 uint32. These names also give a little reminder of the size/range of the intended value.

大多数编译器供应商提供了一个头文件,用于定义具有显式类型大小的原始类型。当代码可能被移植到另一个编译器时,应该使用这些原始类型(在每个实例中都应该读为 ALWAYS)。例如,大多数 UNIX 编译器都有int8_t uint8_t int16_t int32_t uint32_t. 微软有INT8 UINT8 INT16 UINT16 INT32 UINT32. 我更喜欢 Borland/CodeGear 的 int8 uint8 int16 uint16 int32 uint32. 这些名称还提供了对预期值的大小/范围的一点提醒。

For years I have used Borland's explicit primitive type names and #includethe following C/C++ header file (primitive.h) which is intended to define the explicit primitive types with these names for any C/C++ compiler (this header file might not actually cover every compiler but it covers several compilers I have used on Windows, UNIX and Linux, it also doesn't (yet) define 64bit types).

多年来,我一直使用 Borland 的显式原始类型名称和#include以下 C/C++ 头文件 (primitive.h),该文件旨在为任何 C/C++ 编译器定义具有这些名称的显式原始类型(该头文件实际上可能不会涵盖所有编译器,但它涵盖了我在 Windows、UNIX 和 Linux 上使用的几个编译器,它还没有(还)定义 64 位类型)。

#ifndef primitiveH
#define primitiveH
// Header file primitive.h
// Primitive types
// For C and/or C++
// This header file is intended to define a set of primitive types
// that will always be the same number bytes on any operating operating systems
// and/or for several popular C/C++ compiler vendors.
// Currently the type definitions cover:
// Windows (16 or 32 bit)
// Linux
// UNIX (HP/US, Solaris)
// And the following compiler vendors
// Microsoft, Borland/Imprise/CodeGear, SunStudio,  HP/UX
// (maybe GNU C/C++)
// This does not currently include 64bit primitives.
#define float64 double
#define float32 float
// Some old C++ compilers didn't have bool type
// If your compiler does not have bool then add   emulate_bool
// to your command line -D option or defined macros.
#ifdef emulate_bool
#   ifdef TVISION
#     define bool int
#     define true 1
#     define false 0
#   else
#     ifdef __BCPLUSPLUS__
      //BC++ bool type not available until 5.0
#        define BI_NO_BOOL
#        include <classlib/defs.h>
#     else
#        define bool int
#        define true 1
#        define false 0
#     endif
#  endif
#endif
#ifdef __BCPLUSPLUS__
#  include <systypes.h>
#else
#  ifdef unix
#     ifdef hpux
#        include <sys/_inttypes.h>
#     endif
#     ifdef sun
#        include <sys/int_types.h>
#     endif
#     ifdef linux
#        include <idna.h>
#     endif
#     define int8 int8_t
#     define uint8 uint8_t
#     define int16 int16_t
#     define int32 int32_t
#     define uint16 uint16_t
#     define uint32 uint32_t
#  else
#     ifdef  _MSC_VER
#        include <BaseTSD.h>
#        define int8 INT8
#        define uint8 UINT8
#        define int16 INT16
#        define int32 INT32
#        define uint16 UINT16
#        define uint32 UINT32
#     else
#        ifndef OWL6
//          OWL version 6 already defines these types
#           define int8 char
#           define uint8 unsigned char
#           ifdef __WIN32_
#              define int16 short int
#              define int32 long
#              define uint16 unsigned short int
#              define uint32 unsigned long
#           else
#              define int16 int
#              define int32 long
#              define uint16 unsigned int
#              define uint32 unsigned long
#           endif
#        endif
#      endif
#  endif
#endif
typedef int8   sint8;
typedef int16  sint16;
typedef int32  sint32;
typedef uint8  nat8;
typedef uint16 nat16;
typedef uint32 nat32;
typedef const char * cASCIIz;    // constant null terminated char array
typedef char *       ASCIIz;     // null terminated char array
#endif
//primitive.h

回答by Jér?me Radix

The C++ Standardsays it like this :

C ++标准说,它是这样的:

3.9.1, §2 :

3.9.1, §2 :

There are five 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. Plain ints have the natural size suggested by the architecture of the execution environment (44); the other signed integer types are provided to meet special needs.

(44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>.

有五种有符号整数类型:“signed char”、“short int”、“int”、“long int”和“long long int”。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。普通整数具有执行环境架构建议的自然大小 (44);提供其他有符号整数类型以满足特殊需要。

(44) 也就是说,足够大以包含 INT_MIN 和 INT_MAX 范围内的任何值,如标题中所定义 <climits>

The conclusion : it depends on which architecture you're working on. Any other assumption is false.

结论:这取决于您正在使用哪种架构。任何其他假设都是错误的。