问题
单项选择题
有以下程序: int a=2; int f(int n) { static int a=3; int t=0; if(n % 2)(static int a=4; t+=a++;} else{static int a=5; t+=a++;} return t+a++; } matin() { int s=a, i; for(i=0; i<3; i++)s+=f(i); printf("% d\n", s); } 程序运行后的输出结果是______。
A.26
B.28
C.29
D.24
答案
参考答案:C
解析: 在某一函数中当全局变量与局部变量同名时,则在该函数中,全局变量被屏蔽,访问的是局部变量。因此,t+a++中的a用的是static int a=3的这个a。if语句的执行是当n是偶数时,执行else的部分,否则执行其后的部分。因此,当i=0时,t=5,s=2+8=10;i=1时,t=4,s=10+8=18;当i=2时,t=6,s=18+11=29。