c - 为什么0的大小显示为4?

标签 c integer sizeof

我了解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/

相关文章:

c++ - 乘法溢出测试

objective-c - 在 Objective-C 中如何加二?

c++ - 关于 C++ 中 sizeof 运算符的强大功能

c - C 中是否有指针声明约定?

c - 在代码块中出现 "implicit declaration of function"错误

c++ - 堆栈和堆碰撞时会发生什么

c++ - 在 C++ 中将特定格式的数字转换为 long/double

c++ - 为什么结构数组的 memset 会改变程序行为?

c++ - 包含 unordered_map 作为成员的结构的 sizeof()

c - 多进程环境中的多读单写同步?