Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static |
Storage class for block-scoped static objects | L | S | Keyword | C89 | C90 | C95 | C99 | C11 | ||
static |
Specifier for internal functions | L | S | Keyword | C89 | C90 | C95 | C99 | C11 | ||
static |
Storage class for file-scoped static objects | L | S | Keyword | C89 | C90 | C95 | C99 | C11 |
A C program is made from a set of definitions of types, enumeration constants, macros, functions and objects provided in one or more translation units. A translation unit is what you get when you pass a source file through the preprocessor.
- translation-unit
external-declaration
translation-unit external-declaration
All definitions also serve as declarations.
Every definition has either ‘external linkage’, ‘internal linkage’ or ‘no linkage’, which governs what happens when several translation units are combined to produce a complete program. Two definitions with the same name but with different scope can have different linkage.
- External linkage
-
Definitions with external linkage can be referenced by name from anywhere in the same program, even from different translation units. All definitions with external linkage within a complete program must have unique names.
All objects with external linkage have static storage duration.
- Internal linkage
-
Definitions with internal linkage can be referenced by name from anywhere in the same translation unit. All internal and external definitions within a single translation unit must have unique names. This allows a translation unit to have private definitions with linkage that are hidden from other translation units. After translation, their names are often redundant.
All objects with internal linkage have static storage duration.
- No linkage
-
Definitions with no linkage do not survive translation. They inform translation, but the information in its original form is not required thereafter. (However, information about them is often preserved for diagnostic purposes.)
All functions are external by default; it is not necessary
to define them with extern
, and doing so may imply
unwanted additional semantics. A function defined with
static
is internal.
All file-scope objects are external by default.
extern
is redundant, and may imply
unwanted additional semantics. Define with
static
to make them internal.
All block-scope objects have no linkage and
automatic storage
duration by default. Define with
static
to make them internal and
have static storage duration. External
objects cannot be defined in block scope, but extern
applied to a mere declaration
makes it refer to an external object.
All types, enumeration constants, macros, and objects with automatic storage duration always have no linkage.
Enumeration constants will have been replaced with their actual values during phase 7 of translation. Macros are resolved at an even earlier phase. Consequently, enumeration constants and macros always have no linkage.
Types govern what byte offsets and which machine-code instructions of the target language to use; once these qualities have been determined (as a result of translation), type information is often redundant. Types, therefore, always have no linkage.
In the declarations below, counter
,
struct point
, c1
, c2
and all the
automatic objects (the parameters and local variables) have
no linkage; swap
, print_message
and swoon
have internal linkage; f
and
mean
have external linkage:
typedef int counter; // none; type struct point { // none; type int x, y; }; enum { c1, // none; constant c2, // none; constant }; static swap // internal (int *ap, // none; parameter int *bp) // none; parameter { int tmp = *ap; // none; local variable *ap = *bp; *bp = tmp; } static print_message(void); // internal void f(void) // external { static void swoon(void); // internal } double mean // external (const double *start, // none; parameter size_t len); // none; parameter
Functions can be kept local to a translation unit, so their names
aren't visible from other translation units, by declaring
them with static
:
// a static prototype static int sum(int x, int y); // a static definition static int sum(int x, int y) { return x + y; }
Such functions have internal linkage.