The static storage class specifier lets you define objects with static storage duration and internal linkage, or to define functions with internal linkage.
An object having the static storage class specifier can be defined within a block or at file scope. If the definition occurs within a block, the object has no linkage. If the definition occurs at file scope, the object has internal linkage.
Initialization
You can initialize any static object with a
constant expression or an expression that reduces to the address
of a previously declared extern or static
object, possibly modified by a constant expression. If you do not
provide an initial value, the object receives the value of zero
of the appropriate type.
In C++, you can initialize a static data pointer to the address of an imported data object without causing a compiler error.
Storage
Storage is allocated at compile time for static variables that
are initialized. Uninitialized static variables are mapped at
compile time and initialized to 0 (zero) at load time. This
storage is freed when the program finishes running. Beyond this,
the language does not define the order of initialization of
objects from different files.
Block Scope Usage
Use static variables to declare objects that
retain their value from one execution of a block to the next
execution of that block. The static storage
class specifier keeps the variable from being reinitialized each
time the block where the variable is defined runs. For example:
static float rate = 10.5;
Initialization of a static array is performed only once at compile time. The following examples show the initialization of an array of characters and an array of integers:
static char message[] = "startup completed"; static int integers[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
File Scope Usage
The static storage class specifier causes the
variable to be visible only in the file where it is declared.
Files, therefore, cannot access file scope static
variables declared in other files.
If a local static variable is a class object with constructors and destructors, the object is constructed when control passes through its definition for the first time. If a local class object is created by a constructor, its destructor is called immediately before or as part of the calls of the atexit function.
Restrictions
You cannot declare a static function at block
scope.
Block Scope
Data Declaration
File
Scope Data Declarations