有如下程序: #include<iostream> using namespace std; int fun(char x,char y) { if(x>y) return x: return y; } int main() { int a(9),b(8)

admin2021-05-06  21

问题 有如下程序:
#include<iostream>
using namespace std;
int fun(char x,char y)
{
if(x>y)
return x:
return y;
}
int main()
{
int a(9),b(8),c(7);
cout<<fun(fun(a,b),fun(b,c));
return0;
}
运行后的输出结果是(    )。

选项 A、8
B、9
C、7
D、函数调用出错

答案B

解析 程序中,fun()函数的作用是将形参x、y中的较大值作为函数值返回;main()函数首先使用直接初始化方式,初始化a的值为9,b的值为8,c的值为7,然后调用函数:fun(fun(a,b),fun(b,c));并将返回值输出,由于a>b,b>c,所以fun(fun(a,b),fun(b,c))等价于:fun(a,b),也就等价于a。本题答案为B选项。
转载请注明原文地址:https://kaotiyun.com/show/NHfp777K
0

最新回复(0)