我了解sizeof
运算符,但无法理解sizeof(0)
在以下程序中产生4的背景
#include<stdio.h>
int main()
{
printf("%d \n",sizeof(0));
return 1;
}
输出:4
最佳答案
sizeof
返回所提供类型的大小,而不是值。
引用 C11
,章节 §6.5.3.4,(强调我的)
The
sizeof
operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. [...]
现在输入,0
本身就是一个整数常量。
因此,在您的平台上,sizeof(0)
与 sizeof(int)
相同。
也就是说,sizeof
产生了一个 size_t
,因此您应该使用 %zu
格式说明符来打印它。
关于c - 为什么0的大小显示为4?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42711360/