【发布时间】:2011-01-19 15:42:58
【问题描述】:
如何从另一个文件访问静态变量?静态变量没有文件范围吗?
bash-3.2$ ls
a.c b.c
bash-3.2$ cat a.c
#include <stdio.h>
static int s = 100;
int fn()
{
/* some code */
}
bash-3.2$ cat b.c
#include <stdio.h>
#include "a.c"
extern int s;
int main()
{
printf("s = %d \n",s);
return 0;
}
bash-3.2$ gcc b.c
bash-3.2$ a.exe
s = 100
【问题讨论】:
-
静态变量只有文件作用域,不能从其他文件访问,是不是违反或绕过了定义?
-
从不 #include *.c, *.cpp, etc. files as headers.
标签: c static-variables