Extern keyword tell to compiler that this variable is declared in another C file but we can use it in current file
Lets say we have two files
File1.c
File2.c
First method
I have declared x in file 1 and if i want to use x in file 2 I can use but I have to use extern keyword in file 2 to tell the compiler that variable x is already in file 1
Code:
File1.c
int x;
int foo(void)
{
...
}
File2.C
extern int x;
int main(void)
{
x = 5;
...
}
Second method
I have declared x in file 1 and if i want to use x in file 2 I can use but I have to use extern keyword in file 1 to tell the compiler that variable x is already in file 1
Code:
File1.c
extern int x;
int foo(void)
{
...
}
File2.C
int x;
int main(void)
{
x = 5;
...
}
which one is correct method to use extern variable in c file ?