External linkage allows declarations in different translation units to designate the same C object or function. The linker uses this relationship when combining object files, so each reference reaches the corresponding entity. section 13.2
An extern declaration reaches an existing object
A file-scope definition creates an object named i. Inside main, a block-scope declaration using extern does not create the local i again; it refers to the existing externally linked object, so the branch prints 1 rather than the local value 2. section 13.2
TAKEAWAY:
- Use external linkage when multiple object files must access the same object or function. section 13.2
- Do not equate extern with external linkage: extern may refer to identifiers with either external or internal linkage. section 13.2
- Contrast file-scope objects or functions without cross-file connections, which should have internal linkage. section 13.2
Listing 13.2 An example of shadowing with an extern variable
#include <stdio.h> 1
unsigned i = 1;
int main(void) { 2
unsigned i = 2; /* A new object */
if (i) { 3
extern unsigned i; /* An existing object */
printf("%u\n", i); /* prints 1 */
} else {
printf("%u\n", i); /* prints 2 */
}
}
Inspect the complete example showing a local identifier shadowing an externally linked object and an extern declaration restoring access to that object. section 13.2

