다른 파일에서 정적 변수에 액세스하려면 어떻게해야합니까? 정적 변수에 파일 범위가 있습니까?다른 파일에서 정적 변수에 어떻게 액세스 할 수 있습니까?
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
파일 범위 만 있고 다른 파일에서 액세스 할 수없는 정적 변수의 정의를 위반하거나 무시하지 않습니까? – aks
* 결코 * * .c, * .cpp 등의 파일을 헤더로 포함하지 마십시오. –