Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 输出以下代码的原因:_C - Fatal编程技术网

C 输出以下代码的原因:

C 输出以下代码的原因:,c,C,我有几行代码无法理解输出的原因 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; } VisualC++中的P>O/P:-0, 0,0,000000<P/P gcc编译器:-0,0,11.000000您正在使用未定义或未指定的行为。不知道是哪一个。在使用GCC4.7.2的Debian上,我的输出为-780714744、4195886、

我有几行代码无法理解输出的原因

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; 
}
VisualC++中的P>O/P:-0, 0,0,000000<P/P
gcc编译器:-0,0,11.000000

您正在使用未定义或未指定的行为。不知道是哪一个。在使用GCC4.7.2的Debian上,我的输出为-780714744、4195886、11.000000。

当调用变量函数(如
printf
)时,
float
将升级为
double
<代码>整数s按原样传递<因此,当您编写
%f
时,code>printf需要一个
double
,当您编写
%d
时,需要一个
int

因此,不给它一个
double
,而是一个
int
,是未定义的行为。类似地,当函数需要
int
时,传递
double
也是未定义的


像往常一样,未定义的行为意味着“任何事情都可能发生”。永远不要依赖于未定义的行为。

+1,它们都是未定义的。(a) 将浮点值发送到需要int的格式规范,(b)与(a)相同,最后(c)将
int
发送到需要规范字符串的
float
。他们都错了。很好的解释,谢谢,没有比这更好的解释了……)