Nearby lessons
112 of 124C - extern Storage Class
extern declares that a variable or function exists somewhere else — usually in another .c file. Learn the declaration-versus-definition rule that trips everyone up, the header-file pattern, and the linker errors that follow when you get it wrong.
The Core Idea
.c file at a time and knows nothing about the others. extern is your promise that a name will be found later by the linker. It creates no memory — it only lets the current file refer to memory that some other file defines.Declaration vs Definition
This distinction is the whole topic. A definition allocates memory; a declaration only names something:
| Code | Kind | Allocates memory? | How many allowed |
|---|---|---|---|
int x; at file scope | Definition | Yes | One per program |
int x = 5; | Definition | Yes | One per program |
extern int x; | Declaration | No | Any number |
extern int x = 5; | Definition | Yes | One — the initialiser wins |
An Initialiser Turns extern Into a Definition
extern int x = 5; is a definition, not a declaration. The initialiser overrides the extern, so if you write that line in a header included by three files, you get three definitions and a "multiple definition" link error. Headers declare; one .c file defines.The Standard Header Pattern
Every real C project uses this shape. The header carries declarations; one .c file carries the definitions:
extern on Functions Is Redundant
Functions already have external linkage, so all three of these declarations mean the same thing:
extern vs static — Opposites
At file scope the two keywords pull in opposite directions:
extern | static | |
|---|---|---|
| Linkage | External — shared | Internal — private |
| Other files can reach it | Yes | No |
| Allocates memory | No — declaration only | Yes |
| Copies in the program | One, shared | One per file |
| Use for | Genuinely shared state | File-private helpers |
The Two Linker Errors
Almost every extern mistake produces one of these two messages. Knowing which is which saves a lot of time:
| Error | Means | Fix |
|---|---|---|
undefined reference to 'x' | Declared but never defined | Define it in one .c, and compile that file |
multiple definition of 'x' | Defined more than once | Remove the initialiser from the header |
extern in the Same File
extern also works within one file — it lets you use a variable above its definition:
Types Must Match
extern int count; in one file while the other defines long count; and it links happily — then reads the wrong number of bytes at run time, producing garbage that no compiler warning predicted. This is exactly why the extern declaration belongs in a header that both files include: then the compiler checks it for you.Common Mistakes
- Defining instead of declaring in a header —
int x = 5;in a.hgives "multiple definition". - Declaring but never defining — "undefined reference".
- Forgetting to compile the defining file — same error, different cause.
- Mismatched types across files — links cleanly, misbehaves at run time.
- Trying to
externastaticvariable — internal linkage cannot be reached. - Missing include guards — the same header processed twice.
- Overusing shared globals — every file that can change a value is a file you must read when it goes wrong.
.c, and have every file include the header, including the one that defines it. That last part is what turns type mismatches into compile errors instead of run-time mysteries.- extern says "this exists elsewhere" — it does not create storage.
- Define a shared variable in exactly one .c file; declare it extern in a header.
- extern int x; is a declaration; int x; at file scope is a definition.
- Adding an initialiser makes extern a definition, defeating the purpose.
- extern on a function is redundant — functions are external by default.