extern
is used to refer to a variable in another translation unit ("source file").
For instance, your code in main.c looks like this:
extern int var;
int main(void)
{
var = 10;
return 0;
}
This code contains a declaration for an integer named var, but no definition, because extern explicitly says: "the definition for this is somewhere else"
You could define another source file, say, other.c:
int var = 0;
Then, after you add this new translation unit to your build command, your program will link fine, and the code in both files can operate on the shared var
variable.
In the second version, you just override the declaration of the extern var
with a local variable in your main function. Since the extern var
is not (ODR-)used anymore, the linker does not require it, so your executable can build successfully.