在c++中,int和long的区别是什么?

时间:2022-09-01 11:31:31

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字节,大量的值从-2147483648年到2147483647年(2 ^ 31)长是4字节,大量的值从-2147483648年到2147483647年(2 ^ 31)

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

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

9 个解决方案

#1


99  

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 article covers the rules for the Intel C++ compiler on variable platforms. To summarize:

例如,在Windows下它们是相同的,但是在Alpha系统中,长是64位,而int是32位。本文介绍了变量平台上的Intel 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  

#2


73  

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 long guaranteed to be at least 32 bits?

也看:长时间保证至少是32位吗?

#3


13  

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模型,这意味着ints是32位的,而longs是64位的。

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

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

#4


11  

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* ;

有四个带符号整数类型:“带符号字符”、“短int”、“int”和“long int”。在此列表中,每种类型提供的存储空间至少与列表中前面的存储空间相同。普通的ints具有执行环境的体系结构所建议的自然大小*;

[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]

[脚注:也就是说,大到足以包含INT_MIN和INT_MAX范围内的任何值,如标题 所定义。- - -结束foonote]

#5


7  

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_MAX should be at least 2^16-1, USHRT_MAX 2^16-1 and ULONG_MAX 2^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 not derive from that that int is at least 2 byte. Theoretically, char, int and long could all be 1 byte, in which case CHAR_BIT must 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,USHRT_MAX 2 ^ 16:1 ULONG_MAX 2 ^ 32-1。这使得位计数至少为16(短)和int(长),32(长)。对于char,它明确声明它至少应该有8位(CHAR_BIT)。c++继承了这些限制规则。h文件,在c++中我们对这些值有相同的基本要求。但是,您不应该从int至少是2字节中派生出来。理论上,char、int和long都可以是1字节,在这种情况下,CHAR_BIT必须至少是32字节。只要记住“byte”始终是char的大小,所以如果char更大,一个字节就不再是8位了。

#6


5  

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一样大,但是您不能保证它将变得更长。

#7


5  

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标准规定了一些最低要求。

#8


5  

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.

大多数编译器供应商提供一个头文件,该头文件定义具有显式类型大小的原始类型。当代码可能被潜在地移植到另一个编译器时,应该使用这些原始类型(在每个实例中都要这样读)。例如,大多数UNIX编译器都有int8_t uint8_t int16_t int32_t uint32_t。微软已经有了新的目标。我更喜欢Borland/CodeGear的uint8 int16 int16 int32。这些名称还会提醒您预期值的大小/范围。

For years I have used Borland's explicit primitive type names and #include the 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明确的原始类型名称和#包括以下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

#9


4  

The C++ Standard says 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.

有5个带符号整数类型:“带符号字符”、“短int”、“long int”和“long long long long long long int”。在此列表中,每种类型提供的存储空间至少与列表中前面的存储空间相同。普通ints具有执行环境体系结构所建议的自然大小(44);提供了其他带符号整数类型以满足特殊需要。

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

(44)这足够大到可以包含INT_MIN和INT_MAX范围内的任何值,如header 中定义的那样。

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

结论:这取决于您正在处理的架构。任何其他假设都是假的。

#1


99  

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 article covers the rules for the Intel C++ compiler on variable platforms. To summarize:

例如,在Windows下它们是相同的,但是在Alpha系统中,长是64位,而int是32位。本文介绍了变量平台上的Intel 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  

#2


73  

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 long guaranteed to be at least 32 bits?

也看:长时间保证至少是32位吗?

#3


13  

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模型,这意味着ints是32位的,而longs是64位的。

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

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

#4


11  

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* ;

有四个带符号整数类型:“带符号字符”、“短int”、“int”和“long int”。在此列表中,每种类型提供的存储空间至少与列表中前面的存储空间相同。普通的ints具有执行环境的体系结构所建议的自然大小*;

[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]

[脚注:也就是说,大到足以包含INT_MIN和INT_MAX范围内的任何值,如标题 所定义。- - -结束foonote]

#5


7  

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_MAX should be at least 2^16-1, USHRT_MAX 2^16-1 and ULONG_MAX 2^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 not derive from that that int is at least 2 byte. Theoretically, char, int and long could all be 1 byte, in which case CHAR_BIT must 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,USHRT_MAX 2 ^ 16:1 ULONG_MAX 2 ^ 32-1。这使得位计数至少为16(短)和int(长),32(长)。对于char,它明确声明它至少应该有8位(CHAR_BIT)。c++继承了这些限制规则。h文件,在c++中我们对这些值有相同的基本要求。但是,您不应该从int至少是2字节中派生出来。理论上,char、int和long都可以是1字节,在这种情况下,CHAR_BIT必须至少是32字节。只要记住“byte”始终是char的大小,所以如果char更大,一个字节就不再是8位了。

#6


5  

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一样大,但是您不能保证它将变得更长。

#7


5  

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标准规定了一些最低要求。

#8


5  

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.

大多数编译器供应商提供一个头文件,该头文件定义具有显式类型大小的原始类型。当代码可能被潜在地移植到另一个编译器时,应该使用这些原始类型(在每个实例中都要这样读)。例如,大多数UNIX编译器都有int8_t uint8_t int16_t int32_t uint32_t。微软已经有了新的目标。我更喜欢Borland/CodeGear的uint8 int16 int16 int32。这些名称还会提醒您预期值的大小/范围。

For years I have used Borland's explicit primitive type names and #include the 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明确的原始类型名称和#包括以下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

#9


4  

The C++ Standard says 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.

有5个带符号整数类型:“带符号字符”、“短int”、“long int”和“long long long long long long int”。在此列表中,每种类型提供的存储空间至少与列表中前面的存储空间相同。普通ints具有执行环境体系结构所建议的自然大小(44);提供了其他带符号整数类型以满足特殊需要。

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

(44)这足够大到可以包含INT_MIN和INT_MAX范围内的任何值,如header 中定义的那样。

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

结论:这取决于您正在处理的架构。任何其他假设都是假的。