<分区>
为什么下面程序的输出是4?
#include <stdio.h>
int main()
{
printf("%d\n", sizeof('3'));
return 0;
}
<分区>
为什么下面程序的输出是4?
#include <stdio.h>
int main()
{
printf("%d\n", sizeof('3'));
return 0;
}
最佳答案
因为字符常量的类型是int
,而不是char
(而且int
在你的平台上的大小是四)。
C99 规范草案说:
An integer character constant has type int.
这可能看起来很奇怪,但请记住您可以这样做:
const uint32_t png_IHDR = 'IHDR';
换句话说,单个字符常量可以包含多个实际字符(四个,以上)。这意味着生成的 value 不能具有 char
类型,因为那样它会立即溢出并且毫无意义。
注意:上面的内容似乎不是一个很好的好方式,那是另一个讨论。 :)
关于c - 为什么 sizeof ('3' ) == 4 使用 GCC 编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19318022/