TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
extern keyword in C is a storage class specifier used to declare a variable or function without providing its definition. It instructs the compiler that the identifier exists and its memory allocation or implementation is provided elsewhere, leaving an unresolved symbol reference in the object file for the linker to resolve during the final build phase.
Syntax
Core Mechanics
1. Declaration vs. Definition and Tentative Definitions
In C, a definition allocates memory for a variable, while a declaration only informs the compiler of the variable’s name and type.- An uninitialized global variable at file scope (e.g.,
int x;) is a tentative definition. It acts as a declaration that becomes a strict definition at the end of the translation unit if no explicit definition (e.g.,int x = 1;) is provided. - Applying
externwithout an initializer creates a pure declaration. It guarantees to the compiler that the variable exists and memory has been allocated for it in another translation unit or later in the same file.
2. Linkage Rules
Whileextern is generally used to specify external linkage (making the identifier accessible across multiple translation units), its exact behavior depends on prior declarations:
- If no prior declaration is visible,
externestablishes external linkage. - If a prior declaration of the identifier is visible with internal linkage (e.g., previously declared with the
statickeyword), theexterndeclaration inherits that internal linkage rather than establishing external linkage.
3. Storage Duration
Variables declared withextern typically possess static storage duration, meaning they are allocated when the program begins execution and deallocated when it terminates. However, since C11, if a variable is declared with extern _Thread_local, it possesses thread storage duration, meaning a unique instance of the variable is created for each thread.
4. Application to Functions
Functions in C possess external linkage by default. While explicitly applyingextern to a function prototype (e.g., extern void my_func();) is redundant, it is perfectly valid, historically common, and explicitly supported by the C standard.
Code Visualization
File 1:sourceA.c (The Definitions)
sourceB.c (The Declarations)
Technical Caveats
- Initialization Overrides
extern: If anexternvariable is initialized at the time of its declaration, the compiler treats it as a strict definition. Memory is allocated, and theexternkeyword is effectively ignored as a pure declaration.
- Missing Definition: If an
externvariable or function is declared and utilized in the code, but never defined in any linked translation unit, the compilation phase will succeed, but the linker will fail with an “undefined reference” or “unresolved external symbol” error. - Type Matching: The data type in the
externdeclaration must strictly match the data type of the actual definition. The C linker does not perform type checking; mismatched types will result in undefined behavior due to incorrect memory offset calculations or register usage.
Master C with Deep Grasping Methodology!Learn More





