static我有三个文件来演示文件范围中变量的使用。extern变量在 中声明file2.h,在 中初始化file2.c。我正在声明另一个具有相同名称的变量来main.c测试static静态全局范围。但我收到错误消息“main.c|6|error: 'var1' 的静态声明遵循非静态声明。
static有人可以解释一下for 文件范围的用法吗?
如果我不包含file2.h在 中main.c,我不会遇到任何问题。但是,如果我需要使用其他文件的某些函数,main.c但仍希望仅将变量范围保留到该文件,该怎么办?
主程序
#include <stdio.h>
#include "file2.h"
static int var1;
int main()
{
printf("value of staticVar1 = %d\n",var1);
func1();
printf("value of staticVar1 after function call= %d\n",var1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
文件2.h
#ifndef _FILE2_H
#define _FILE2_H
#include <stdio.h>
extern int var1;
int func1(void);
#endif // _FILE2_H
Run Code Online (Sandbox Code Playgroud)
文件2.c
#include <stdio.h>
#include "file2.h"
int var1=3;
int func1(void)
{
printf("value …Run Code Online (Sandbox Code Playgroud)