有以下程序#include<stdio.h>int fun(){static int x=1;x+=1;return x;}main(){int i,s=
有以下程序
#include<stdio.h>
int fun()
{static int x=1;
x+=1;return x;
}
main()
{int i,s=1;
for(i=1;i<=s;i++)s+=fun();
printf(”%d\n”,s);
}
程序运行后的输出结果是()。
A.11
B.21
C.6
D.120
B
解析:本题考查静态变量,C语言中静态变量的生存期是从程序的开始到结束。本题中静态变量x,随着每次进入循环不断增长5次,返回的值分别为2、3、4、5、6。所以s=1+2+3+4+5+6=21。所以选B。