如果我错了,请纠正我,
int类型占用4个字节,取值范围为-2,147,483,648到2,147,483,647(2的31次方)
long类型占用4个字节,取值范围为-2,147,483,648到2,147,483,647(2的31次方)
C++中有什么区别吗?它们可以互换使用吗?
这是实现相关的。例如,在Windows下它们是相同的,但在Alpha系统上,long为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
你所拥有的唯一保证是:
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位模式下,int为32位,而long为64位。
例如,MSVC使用LLP64模型,这意味着即使在64位模式下,int和long都是32位。
C++规范本身(虽然是旧版本但足够好)并未确定这一点。
有四种带符号的整数类型: '
signed char
', 'short int
', 'int
',和'long int
'。在此列表中,每个类型提供的存储空间至少与其前面的类型相同。纯整数具有由执行环境体系结构建议的自然大小*;[注释:也就是说,足够大以包含头文件
<climits>
中定义的INT_MIN和INT_MAX范围内的任何值。--- 结束注释]
UINT_MAX
至少应为2^16-1,USHRT_MAX
为2^16-1,ULONG_MAX
为2^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位。byte
数据类型。它确实不存在,对吧?如果存在,并且byte
可以有除了8位之外的其他大小,那就纯粹是愚蠢的。既然不是8位,为什么还要称其为字节呢? - Alderath这取决于你的编译器。你可以保证long至少和int一样大,但不能保证它会更长。
C++标准如下所述:
3.9.1,§2:
共有五种带符号整数类型: "signed char"、"short int"、"int"、 "long int" 和 "long long int"。在此列表中, 每个类型提供的存储空间至少与前面的类型相同。 普通的 int 具有执行环境体系结构建议的自然大小(44); 其他带符号整数类型是为了满足特殊需要而提供的。
(44) 即,足够大,能够容纳头文件
<climits>
中 INT_MIN 和 INT_MAX 定义范围内的任何值。
结论:它取决于你正在使用哪种体系结构。任何其他假设都是错误的。
#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
#include <stdint.h>
和指定大小的类型,例如uint32_t
。在新平台上,您只需要确保 stdint.h 对于该特定平台正确即可,这样您的代码就能按照预期工作。 - BitTickler