`int` 一定是 4 字节

/ dousha99

不是。sizeof(int) 随平台的不同会有不同的结果。

C 语言标准

C2y 标准草案第 6.2.5 节:

A "plain" int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as 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**cx51cc2
x86***4
x86_64***4
xtensaesp32**4

正在加载评论……

发表评论

您的评论将由管理员审核后方可公开显示。

Your comments will be submitted to a human moderator and will only be shown publicly after approval.