32位环境涉及"ILP32"数据模型,是因为C数据类型为32位的int、long、指针。而64位环境使用不同的数据模型,此时的long和指针已为64位,故称作"LP64"数据模型。
当然有跨平台的方法:
现今所有64位的类Unix平台均使用LP64数据模型,而64位Windows使用LLP64数据模型,除了指针是64位,其他基本类型都没有变。
TYPE LP32 ILP32 LP64 ILP64 LLP64
char 8 8 8 8 8
short 16 16 16 16 16
int 16 32 32 64 32
long 32 32 64 64 32
long long 64 64 64 64 64
pointer 32 32 64 64 64
int64_t用来表示64位整数,在32位系统中是long long int,在64位系统中是long int,所以打印int64_t的格式化方法是:
- printf("%ld", value); // 64bit OS
- printf("%lld", value); // 32bit OS
- #include <inttypes.h>
- printf("%" "PRId64" "\n", value);
- // 相当于64位的:
- printf("%" "ld" "\n", value);
- // 或32位的:
- printf("%" "lld" "\n", value);