我有几行代码我无法理解这个输出的原因..
int main()
{
int a=5;
float b=10.5,c=11.0;
printf("%d",b);
printf("\n%d",c);
printf("\n%f",a);
return 0;
}
Visual C++ 中的 O/p :- 0 ,0 ,0.000000
gcc 编译器:- 0,0, 11.000000
我有几行代码我无法理解这个输出的原因..
int main()
{
int a=5;
float b=10.5,c=11.0;
printf("%d",b);
printf("\n%d",c);
printf("\n%f",a);
return 0;
}
Visual C++ 中的 O/p :- 0 ,0 ,0.000000
gcc 编译器:- 0,0, 11.000000
当您调用可变参数函数printf
时,float
s 会升级为double
. int
s 按原样传递。printf
因此期望 a double
when you write%f
和 an int
when you write %d
。
不给它一个double
,而是一个int
,因此是未定义的行为。double
同样,当函数需要 an 时传递 aint
也是未定义的。
像往常一样,未定义的行为意味着“任何事情都可能发生”。永远不要依赖未定义的行为。
您正在玩未定义或未指定的行为。不确定是哪一个。在我的带有 gcc 4.7.2 的 Debian 上,我得到 -780714744, 4195886, 11.000000 的输出。