extern External linkage declarations
extern External linkage declarations
extern declares external names. Uninitialized file-scope objects are not defined; sharing needs one matching definition, while initialized extern defines.
| Category | Storage-class specifiers |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
extern int g_count; /* Declaration, not a definition */
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
shared value = 42
ℹ️ Info To keep this single-file example runnable, shared_value is first declared with extern and defined exactly once at the end. Multi-file projects usually put the declaration in a header and the definition in one source file.
🏷️ Related storage-class specifiers
📚 Related Tutorials
❓ FAQ
QHow do extern int x and int x differ at file scope?
Aextern int x is normally only a declaration, while int x without a storage-class specifier is a tentative definition that can produce a zero-initialized definition. A shared object must ultimately have exactly one definition.
QWhere should a variable declared with extern be defined?
AProvide exactly one definition with a compatible type in a translation unit linked into the program. Other translation units refer to it through extern declarations. It need not be in a different physical file, but the program must satisfy the one-definition rules.
QIs extern int x = 1 a declaration or a definition?
AAt file scope, an extern declaration with an initializer is also a definition and allocates the object. It is not merely a reference to another definition. Avoid this form in headers shared across files because it can cause multiple definitions.