If those two files, plus a header containing only extern int x;
, are all you have, it shouldn't even compile (well, it might compile but it won't link).
extern int x;
lets the compiler know that x
exists somewhere but doesn't actually bring it into existence.
The way this is normally done is to define the variable somewhere and declare it wherever it's used, something like:
project.h:
extern int x; // declare
file1.c:
#include "project.h" // declare in the header
int main (void) {
x = 200;
printf ("x is %d\n", x);
return 0;
}
file2.c:
#include "project.h" // declare in the header
int x; // define it.