static Storage Class Specifier

The static storage class specifier lets you define objects or functions with internal linkage, which means that each instance of a particular identifier represents the same object or function within one file only. In addition, objects declared static have static storage duration, which means that memory for these objects is allocated when the program begins running and is freed when the program terminates.

Static storage duration for an object is different from file or global scope: an object can have static duration but local scope. On the other hand, the static storage class specifier can be used in a function declaration only if it is at file scope.

The static storage class specifier can only be applied to the following names:

You cannot declare any of the following as static:

C The keyword static is the major mechanism in C to enforce information hiding. C++ enforces information hiding through the namespace language feature and the access control of classes.

At the C99 language level, the static keyword can be used in the declaration of an array parameter to a function. The static keyword indicates that the argument passed into the function is a pointer to an array of at least the specified size. In this way, the compiler is informed that the pointer argument is never null.

C++ The use of the keyword static to limit the scope of external variables is deprecated for declaring objects in namespace scope.

Initialization

You initialize a 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 explicitly initialize a static (or external) variable, it will have a value of zero of the appropriate type.

C More precisely, in C,

A static variable in a block is initialized only one time, prior to program execution, whereas an auto variable that has an initializer is initialized every time it comes into existence.

Each time a recursive function is called, it gets a new set of auto variables. However, if the function has a static variable, the same storage location is used by all calls of the function.

C++ A static object of class type will use the default constructor if you do not initialize it. Automatic and register variables that are not initialized will have undefined values.

In C++, you may initialize a static object with a non-constant expression, but the following usage has been deprecated:

static int staticInt = 5;
int main()
{
   // . . .
}

C++ provides the namespaces language feature to limit the scope of external variables.

Linkage

A declaration of an object or file that contains the static storage class specifier and has file scope, gives the identifier internal linkage. Each instance of the particular identifier therefore represents the same object or function within one file only.

Example

Suppose a static variable x has been declared in function f(). When the program exits the scope of f(), x is not destroyed. The following example demonstrates this:

#include <stdio.h>
 
int f(void) {
  static int x = 0;
  x++;
  return x;
}
 
int main(void) {
  int j;
  for (j = 0; j < 5; j++) {
    printf("Value of f(): %d\n", f());
  }
  return 0;
}

The following is the output of the above example:

Value of f(): 1
Value of f(): 2
Value of f(): 3
Value of f(): 4
Value of f(): 5

Because x is a static variable, it is not reinitialized to 0 on successive calls to f().

Related References

IBM Copyright 2003