1 //test.c
2
3 #include <stdio.h>
4 extern int global_var;
5
6 void test_global_var()
7 {
8 global_var++;
9 printf("global_var = %d\n", global_var);
10 }
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 void test_static_local_variable();
6
7 int global_var = 1; //普通全局变量,随着整个程序的结束而消亡。可以在整个程序方法问
8 //可以在其他.c文件中访问
9 static int static_global_var = 1; //静态全局变量,限定只能在本文件内部访问
10
11 int main(int argc, char** argv)
12 {
13 int a = 3; //普通局部变量,只能在main函数内部使用,随着main函数的结束而消亡
14
15 for (int i = 0; i < a; i++) //复合语句中定义,随着for循环的结束而消亡
16 {
17 printf("i = %d\n", i);
18 }
19
20 test_static_local_variable(); //local_var = 1
21 test_static_local_variable(); //local_var = 2
22 test_static_local_variable(); //local_var = 3
23
24 printf("global_var = %d\n", global_var); //global_var = 1
25 test_global_var(); //global_var = 2
26 test_global_var(); //global_var = 3
27
28 system("pause");
29 return 0;
30 }
31
32 void test_static_local_variable()
33 {
34 static int local_var = 0; //静态局部变量,只能在函数test_static_local_variable内部使用
35 //生命周期为整个程序,随着程序的结束而消亡
36 local_var++;
37 printf("local_var = %d\n", local_var);
38 }