在 C 语言中 double, long, unsigned, int, char 类型数据所占字节数和机器字长及编译器有关系, 所以, int, long int, short int 的宽度都可能随编译器而异.
但有几条铁定的原则 (ANSI/ISO 制定的):
1. sizeof(shot int) <= sizeof(int)
2. sizeof(int) <= sizeof(long int)
3. short int 至少为 16 位 (2字节)
4. long int 至少应为 32 位
unsigned 是无符号的意思.
例如:
16 位编译器
char: 1 字节 char*(即指针变量): 2 字节 short int: 2 字节 int: 2 字节 unsigned int: 2 字节 float: 4 字节 double: 8 字节
long: 4 字节 long long: 8 字节 unsigned long: 4 字节
32 位编译器
char: 1 字节 char*(即指针变量): 4 字节(32 位的寻址空间是 2^32, 即 32 个 bit, 也就是 4 字节. 同理 64 位编译器)
short int: 2 字节 int: 4 字节 unsigned int: 4 字节 float: 4 字节 double: 8 字节
long: 4 字节 long long: 8 字节 unsigned long: 4 字节
64 位编译器
char: 1 字节 char*(即指针变量): 8 字节 short int: 2 字节 int: 4 字节 unsigned int: 4 字节 float: 4 字节 double: 8 字节
long: 4 字节 long long: 8 字节 unsigned long: 8 字节