The keyword extern means "declare without definition." In other words, this is a way to explicitly declare a variable or force to declare without a definition.
So, in file2 you simply declared a variable without definition (no memory allocated). In file1 you declared and defined an integer variable. Here you allocated memory on the BSS segment because you have an uninitialized global (for C).
In C ++, global tables are stored in an area for each process .
Difference between declaration and definition:
To understand how external variables relate to the extern keyword, you need to know the difference between the definition and declaration of a variable.
When a variable is defined, the compiler allocates memory for this variable and, possibly, also initializes its contents to a certain value. When a variable is declared, the compiler requires that the variable be defined elsewhere.
The declaration tells the compiler that a variable with this name and type exists, but the compiler does not need to allocate memory for it, since it is allocated elsewhere.
Pierre fourgeaud
source share