有以下程序: 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

admin2013-02-23  14

问题 有以下程序:    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

答案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。
转载请注明原文地址:https://kaotiyun.com/show/rhJp777K
0

随机试题
最新回复(0)