Quiz Entry - updated: 2026.07.14
What is the difference between extern and non-extern variable declarations?
extern only declares a variable (no storage) — it promises the definition lives in another file; without extern you define it, allocating storage.
// file1.c
// DEFINITION: allocates storage
int global = 42;
// file2.c
// DECLARATION: uses storage from file1.c
extern int global;
void foo() {
// Works!
printf("%d", global);
}
Without extern:
// file1.c
// Definition
int global = 42;
// file2.c
// Another definition! (weak/tentative)
int global;
// May work (Rule 2/3) but dangerous
Best practices:
| Situation | In header | In source |
|---|---|---|
| Global variable | extern int x; |
int x = 0; |
| Global function | void func(void); |
void func(void) {...} |
Why extern matters:
// Without extern, each file gets its own copy!
// This is a common source of subtle bugs
// myheader.h
// BAD: defines in every file that includes it!
int counter;
// myheader.h (correct)
// Good: declaration only
extern int counter;
// counter.c
// Single definition
int counter = 0;