不是。sizeof(int) 随平台的不同会有不同的结果。
C 语言标准
C2y 标准草案第 6.2.5 节:
A "plain"
intobject has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the rangeINT_MINtoINT_MAXas defined in the header<limits.h>).
C2y 标准草案附录 E.1:
For the following macros, the minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign that are deduced from the prior macros as indicated.
/// <SNIP> ///
#define INT_MIN -32678
#define INT_MAX 32767
/// <SNIP> ///
也就是说,C 语言标准仅指定了 int 的最小大小是 2 字节,剩下的由平台厂商和编译器开发者自行发挥。
平台实现
所以就还是有许多不同的。
| 平台 | 芯片 | 操作系统 | 编译器 | 行为 | 备注 |
|---|---|---|---|---|---|
aarch64 | * | * | * | 4 | |
arm | * | * | * | 4 | |
avr | * | * | * | 2 | |
c51 | * | * | cx51cc | 2 | |
x86 | * | * | * | 4 | |
x86_64 | * | * | * | 4 | |
xtensa | esp32 | * | * | 4 |
cx51cc: Keil Cx51 编译器
正在加载评论……