Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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

extern data_type variable_name;
extern return_type function_name(parameter_types);

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 extern without 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

While extern 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, extern establishes external linkage.
  • If a prior declaration of the identifier is visible with internal linkage (e.g., previously declared with the static keyword), the extern declaration inherits that internal linkage rather than establishing external linkage.

3. Storage Duration

Variables declared with extern 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 applying extern 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)
// Memory is allocated here. 
// These are the definitive sources of truth for the linker.
int system_status = 1; 

void trigger_alert(void) {
    // Function implementation
}
File 2: sourceB.c (The Declarations)
// Pure declarations. No memory is allocated.
// The compiler allows the use of these identifiers based on these signatures.
extern int system_status; 
extern void trigger_alert(void); 

void check_status(void) {
    if (system_status == 1) {
        // The linker resolves system_status and trigger_alert to sourceA.c
        trigger_alert();
    }
}

Technical Caveats

  • Initialization Overrides extern: If an extern variable is initialized at the time of its declaration, the compiler treats it as a strict definition. Memory is allocated, and the extern keyword is effectively ignored as a pure declaration.
extern int count = 100; // WARNING: This is a definition, not a pure declaration.
If this occurs in a header file included by multiple translation units, the linker will throw a “multiple definition” error.
  • Missing Definition: If an extern variable 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 extern declaration 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