c - 为什么程序的输出是这样呢?

标签 c

# 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) 解决了完全相同的问题:

http://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_083.html

关于c - 为什么程序的输出是这样呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17907992/

相关文章:

c++ - 我可以将任意源 IP 绑定(bind)到套接字吗?

c - 指针减法的机制

c - 以附加模式打开文件 : using open() API

C语言-char[]输出中的 "Weird"个字符

c - 什么触发了 0x08 中断?

c - 我如何让 C 编译器安静下来,让函数指针接受任意数量的参数?

C 结构/函数交叉引用

c - 在双指针上使用 strcpy 时出现段错误

C 或 '|' 运算符

c - 使用c编程在nano linux中进行Ping 6程序