static type definition
s in C
somewhat different purpose
s. In all cases, they define an item of data
which is global
in the sense that only one copy
exists for the entire runtime
of the program
. It can be viewed as setting a peculiar "local
for a global variable
The scope of an object defined
static follows the usual scoping rules for C: for a variable, till the end of the block (typically the function) in which it is defined, or till the end of the file, if defined outside any functions; for a function, merely till the end of the file (in C, functions cannot be defined inside functions). This aspect of staticness refers to the visibility of the object. For a function definition (or declaration), this is the only effect -- functions don't have storage types.
The storage type of a variable defined
static is global, even in an inner block scope rather than file scope. This means that the variable retains its value between calls to the function. This behaviour is very different from the default
automatic status of variables defined in a block, which only exist inside the block. In particular, you may freely return a pointer to (or into) a static variable's storage from a function, but you cannot do so for an automatic variable.