问题 单项选择题

有以下程序:
float fun(int x, int y)
return(x+y);
main()
int a=2, b=5, c=8;
printf("% 3.0f\n", fun((int)fun(a+c, b) , a-c) );

程序运行后的输出结果是______。

A.编译错误

B.9

C.21

D.9.0

答案

参考答案:B

解析: 函数fun的功能是求两个数之和,其形参数据类型为int型,函数的返回值类型是float型。fun((int)fun(a+c,b),a-c)中有两层函数调用,其中内层调用是fun(a+c,b),因a+c+b=2+8+5=15,而返回类型是float型,所以fun(a+c,b)的值为15.0,将该值强制变换为int型后作为外层函数调用的参数,即fun(15,a-c)=15+a-c=15+2-8=9.0。因printf语句的输出格式为"%3.0f",即小数点后0位小数,故程序的输出为9。

填空题
计算题