我正在尝试一个小例子来了解静态外部变量及其用途.静态变量属于局部范围,外部变量属于全局范围.
static5.c
#include<stdio.h>
#include "static5.h"
static int m = 25;
int main(){
func(10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
static5.h
#include<stdio.h>
int func(val){
extern int m;
m = m + val;
printf("\n value is : %d \n",m);
}
Run Code Online (Sandbox Code Playgroud)
gcc static5.c static5.h
o/p:
static5.c:3: error: static declaration of m follows non-static declaration
static5.h:3: note: previous declaration of m was here
Run Code Online (Sandbox Code Playgroud)
EDITED
正确的程序:
a.c:
#include<stdio.h>
#include "a1_1.h"
int main(){
func(20);
return 0;
}
a1.h:
static int i = 20;
a1_1.h:
#include "a1.h"
int func(val){
extern …Run Code Online (Sandbox Code Playgroud) c ×1