Each compiler is free to choose appropriate sizes for its own hardware, subject only to the restriction that shorts and ints are at least 16bits, longs are at least 32bits, and short is no longer than int, which is no longer than long。
从这段英文中可以看出基本的数据类型占用的字节多少与编译器和操作系统位数都有关。对于当今的主流编译器,在32位和64位操作系统下,int占4个字节。
测试代码如下,编译器XCode,操作系统Mac OS 64位。
#include <iostream>
int main(int argc, const char * argv[]) {
int n = sizeof(int);// 4
return 0;
}