#include <stdio.h> //默认全局变量为外部变量 int a; //当全局变量前面加上static时,该变量为内部变量 static int b; void test(); int main() { /************************************************************* * extern static和全局变量 * 全局变量分2种: * 外部变量:定义的变量能被本文件和其他文件访问 * 1、 默认情况下,所有的全局变量都是外部变量 * 2、 不同文件中的同名外部变量,都代表着同一个变量 * 内部变量:定义的变量只能被本文件访问,不能被其他文件访问 * 1、 不同文件中的同名内部变量,互不影响 * static对变量的作用:定义一个内部变量 * extern对变量的作用:声明一个外部变量 * *************************************************************/ a = 20; b = 1000; test(); return 0; }
#include <stdio.h> int a; int b; void test() { printf("a = %d\n", a); printf("b = %d\n", b); }
a = 20 b = 0
原文:http://www.cnblogs.com/heml/p/3531772.html