Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
static |
Multipurpose modifier | L | Keyword | C89 | C90 | C95 | C99 | C11 |
The keyword static
doesn't have a single clear use. The meaning depends on
context.
-
It can specify static storage duration, indicating that an object exists for the duration of the program's execution.
-
It can specify internal linkage, indicating that an object with static storage, or a function, is not visible outside of its translation unit.
-
It can indicate a minimum size for an arrays passed to a function.
The following skeleton of code shows four uses of
static
:
static int counter; static void func(int sz, double arr[static 10]) { static double sum; }
sum
has block scope because it is declared inside a
block statement.
static
gives it
static storage duration, so it is
created once at the start of the program's execution, and
deleted when program execution ends. Without it, sum
would have automatic storage duration, so a new object would
be created on each entry to the function, and destroyed when
the function returned.
counter
has file scope, so it would already have
static storage duration even without
static
, but the
extra keyword means that counter
has
internal rather than external linkage. When static
is applied
to a function, such as func
, the
function has internal rather than external linkage.
The static
that appears
in the parameter list of func
indicates that the second argument (assigned to arr
) must be a pointer to the first of at least
10
elements of an array, or the
behaviour would be undefined. (This feature is only available from
C99.)