我有以下代码片段,我必须分析输出是什么:
#include <stdio.h>
void f(int d);
int a = 1, b = 2, c = 3, d = 4;
int main(){
int a = 5, c = 6;
f(a);
f(b);
f(c);
printf("%d %d %d %d\n",a,b,c,d);
return 0;
}
void f(int d){
static int a = 0;
a = a + 7;
b = a + d;
c++;
d--;
printf("%d %d %d %d\n",a,b,c,d);
}
我得到的输出如下:
7 12 4 4
15 26 5 11
21 27 6 5
5 27 6 4
这确实让我很困惑。我注意到在所有 3 个函数调用中全局声明的a
接受任务并且在printf()
from main()
身体的a
声明于main()
被打印。但是,我不确定其余变量的行为。这是未定义的行为还是实际上有意义?
int a = 1, b = 2, c = 3, d = 4;
---> 全局变量
int main(){
int a = 5, c = 6; ---> Shadows the global `a` and `c`
....
void f(int d){
static int a = 0; ---> local static variable visible only inside `f`
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)