Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

Linkage in C

Reference for linkage in C: external, internal and no linkage, static and extern at file scope, tentative definitions, one definition rule and multi-file builds.

Linkage decides whether the same name in different places refers to the same object or function. C has three kinds. A name with external linkage refers to one entity across the whole program, even across source files. A name with internal linkage refers to one entity within a single translation unit (a .c file after preprocessing). A name with no linkage, such as an ordinary local variable, is unique to its declaration.

By default, file-scope functions and variables have external linkage. Adding static gives them internal linkage, hiding them from the rest of the program. The keyword extern declares a name defined elsewhere.

C
int counter = 0;           /* external linkage: visible to other files */
static int hidden = 0;     /* internal linkage: this file only */
extern int shared;         /* declaration of something defined in another file */

Try it

C Internal and external names in one file

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 Linkage of locals and the extern block trick

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.

Single-file examples cannot show the real purpose of linkage, which is splitting a program into files. The next listing is read-only:

C
/* counter.h */
extern int counter;               /* declaration: every file may include this */
void bump(void);

/* counter.c */
#include "counter.h"
int counter = 0;                  /* the only definition */
static int internal_calls = 0;    /* invisible to other files */
void bump(void) { counter++; internal_calls++; }

/* main.c */
#include "counter.h"
int main(void) { bump(); return counter; }

Syntax

C
int x;                  /* file scope: external linkage, tentative definition */
static int x;           /* file scope: internal linkage */
extern int x;           /* declaration; takes the linkage of a visible earlier declaration, else external */
static void f(void);    /* function with internal linkage */
void f(void);           /* function: external linkage (extern is implied) */
Declaration Linkage
File-scope object or function without static External.
File-scope static Internal.
File-scope const object (not static) External in C (unlike C++, where it is internal).
extern on a later declaration Same as the earlier visible declaration; external if none.
Local variable, parameter, typedef name, enum constant None.
register, or local static None (local static only changes duration).

Errors / undefined behavior: two external definitions of the same name in different files give a linker error ("multiple definition"), and the language rule is that an identifier with external linkage must have exactly one definition in the program. Declaring the same entity with incompatible types in different files is undefined. Using static and external linkage for the same name in one file is a compile error.

Usage notes

  • Default to static for functions and variables used by one file. It avoids name clashes, helps the optimizer (it can inline and remove unused functions), and documents what is private.
  • Headers declare, source files define. Put extern int counter; and function prototypes in headers, and the single definition in one .c file.
  • Tentative definitions. int x; at file scope is a tentative definition: it may repeat in one file, and compilers before GCC 10 merged repeated ones across files ("common symbols"). GCC 10 and later default to -fno-common, so duplicates now fail at link time. Do not rely on merging.
  • Do not define variables in headers. int counter = 0; in a header gives every including file a definition.
  • inline functions have special linkage rules; see Inline functions.
  • Naming collisions with libraries. Externally linked names share one space with the C library and other libraries. Use a prefix (mylib_parse) for public names.
  • Shared libraries can restrict exported symbols further, with compiler options or visibility attributes; that is a platform feature, not part of the C language.
  • Block-scope extern declarations are legal, but a file-scope declaration in a header is cleaner.