Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Keywords and Statements

The extern keyword

Reference for the C extern keyword: declaring objects and functions defined elsewhere, sharing globals between files, and declaration vs definition.

extern declares that an object or function exists, with external linkage, without defining it (no storage is reserved by that declaration). The definition lives in exactly one translation unit; every other file that needs the name includes a matching declaration, usually from a header.

Functions already have external linkage by default, so extern int f(void); and int f(void); mean the same. For variables the keyword matters: int count; at file scope can be a definition, while extern int count; is only a declaration.

C
/* config.h */
extern int verbosity;           /* declaration */

/* config.c */
int verbosity = 1;              /* the one definition */

Try it

C Declaration first, definition later

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C extern inside a block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
extern type name;                    /* object declaration */
extern return-type name(params);     /* function declaration */
Form Meaning
extern int x; Declaration only; no storage.
extern int x = 5; A definition (with a warning in GCC): an initializer turns it into one.
int x; at file scope Tentative definition; becomes a definition at the end of the file if nothing else defines it.
extern in a block Refers to the file-scope object of that name with external linkage.

Errors: two definitions of the same name in different files give a linker error ("multiple definition"). A declaration with no definition anywhere gives "undefined reference". Declaring the same name with incompatible types in two files is undefined behavior that the linker will not catch.

Usage notes

  • Put extern declarations in a header and include it from the defining file too, so the compiler checks the types match.
  • Before GCC 10, tentative definitions in several files were silently merged ("common symbols"). Since GCC 10 -fno-common is the default and they are errors. Do not rely on merging.
  • Global variables are a design cost: shared mutable state is hard to test and unsafe across threads. Prefer functions that hide the data, see opaque types.
  • extern const is the usual way to share a constant: extern const int max_users; in the header, the initialized definition in one .c file.
  • Arrays: extern int table[]; is a valid incomplete declaration; sizeof becomes unusable until the size is visible.
  • To make a name private use static. C has no extern "C"; that is a C++ construct that headers shared with C++ wrap in #ifdef __cplusplus.
  • Link-time errors are about the build, not the compiler; see compiling and linking.