有以下程序: #include int f(int n); main( ) {int a=3,s; S=f(a);s=s+f(a);printf("%d\n",s); } int f(int n)

admin2021-07-09  21

问题 有以下程序:
    #include  
    int f(int n);
    main( )
    {int a=3,s;
    S=f(a);s=s+f(a);printf("%d\n",s);
    }
    int f(int n)
    {  static int a=1;
    n+=a++;
    return n;   
    }
    程序运行后的输出结果是(    )。

选项 A、9
B、8
C、7
D、10

答案A

解析 题目中的静态局部变量a,在静态存储区内分配存储单元,在程序整个运行期间都不释放。所以第一次调用函数执行n+=a++;时a先与n相加在再进行自增。n的值为4,a的值为2,且a变量执行完后空间没有释放。再执行s=s+f(a)时,s的值为4,调用f(a)函数时n的返回值为n=3+2=5,此时a的值为3。所以s的值为9。
转载请注明原文地址:https://kaotiyun.com/show/ZEkp777K
0

最新回复(0)