如果我错了,纠正我,
int是4个字节,从-2,147,483,648到2,147,483,647(2 ^ 31)
长的值范围是4个字节,值范围从-2,147,483,648到2,147,483,647(2 ^ 31)
C++有什么区别?它们可以互换使用吗?
Rob*_*ker 102
它取决于实现.
例如,在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
Run Code Online (Sandbox Code Playgroud)
Mar*_*ork 79
您唯一的保证是:
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
Run Code Online (Sandbox Code Playgroud)
Adr*_*ian 13
编译x64时,int和long之间的差异介于0到4个字节之间,具体取决于您使用的编译器.
GCC使用LP64模型,这意味着ints是32位,但long在64位模式下是64位.
例如,MSVC使用LLP64模型,这意味着即使在64位模式下,int和long也都是32位.
正如Kevin Haines指出的那样,整数具有执行环境建议的自然大小,其必须符合INT_MIN和INT_MAX.
C89标准规定UINT_MAX
应至少为2 ^ 16-1,2 USHRT_MAX
^ 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.只记得"byte"总是char的大小,所以如果char更大,一个字节不仅仅是8位更多.
在大多数情况下,字节数和值范围由CPU的体系结构决定,而不是由C++决定.但是,C++设定了最低要求,这些要求正确解释,而马丁约克只是犯了一些错误.
你不能互换地使用int和long的原因是因为它们并不总是相同的长度.C是在PDP-11上发明的,其中一个字节有8位,int是两个字节,可以直接用硬件指令处理.由于C程序员经常需要四字节算术,因此发明了很长时间,并且它是由字库函数处理的四个字节.其他机器有不同的规格.C标准规定了一些最低要求.
如果您在其他机器架构,操作系统或其他供应商的编译器上编译代码,那么依赖于编译器供应商的原始类型大小的实现将会困扰您.
大多数编译器供应商提供了一个头文件,用于定义具有明确类型大小的基本类型.当代码可能被移植到另一个编译器时,应该使用这些原始类型(在每个实例中都将其视为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
.这些名称还提示了预期值的大小/范围.
多年来,我一直使用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
Run Code Online (Sandbox Code Playgroud)