I've encountered many examples on the web, that have extern int x
in main.c, where the main function lies.
The only use of extern
I'm aware of, is to make a declaration in another file, and use it in another file, after defining it.
Like :
a.h : extern int x;
a.c : int x = 5;
main.c :
#include "a.h" // and start using x
The 1st case seems redundant to me.
So,
Is there any possible use of using an extern
variable in a file that is not included in any other file?
extern
tells the compiler that x
exists in a different module and should be linked from elsewhere. Putting it in main.c directly just avoids pulling in a header (which would be included in-line anyways)
Just like in a header, x
still needs to exist in another .c module where it isn't defined extern
.