I have two C files 1.c and 2.c
2.c
#include<stdio.h>
static int i;
int func1(){
i = 5;
printf("\nfile2 : %d\n",i);
return 0;
}
1.c
#include<stdio.h>
int i;
int main()
{
func1();
printf("\nFile1: %d\n",i);
return 0;
}
I compiled both the files with “gcc 1.c 2.c -o st”
The output is as follows
file2 : 5
File2: 0
I was expecting output as follows
file2 : 5
File2: 5
I want to access the same variable “i” in both the files.
How can I do it?
Choose one file which will store the variable. Do not use
static. The whole point ofstaticis to keep the variable private and untouchable by other modules.In all other files, use the
externkeyword to reference the variable: