# include <stdio.h>
int main()
{
int a=4;
printf("%f",a);
}
输出
0.000000
还有
# include <stdio.h>
int main()
{
float a=4.5;
printf("%d",a);
}
输出
0
任何人都可以解释上述输出的行为吗? 我知道使用不同的转换规范是愚蠢的,但我只是询问理论目的。
最佳答案
Can anyone explain the behaviour of the above outputs ?
这是未定义的行为。
(C11, 7.1.4p1) "If an argument to a function has [...] or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined"
出于历史原因,有一份 C 缺陷报告 (DR#83) 解决了完全相同的问题:
关于c - 为什么程序的输出是这样呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907992/