Storage-Class Specifiers
Five keywords are described as storage-class specifiers:
typedef
-
The keyword typedef is called a storage-class specifier for syntactic convenience only.
static
-
The keyword static can be a source of confusion because its meaning varies depending on the
scope in which it is used.
-
At block scope, static specifies
that an object is allocated with static storage duration. For such an object, storage is reserved
and initialised only once, prior to program startup. The object exists
and retains its value throughout the execution of the program. Without
the static storage class specifier, an object has automatic storage duration
which means that it is recreated (and possibly
reinitialised) each time the block is entered; it is not preserved when
execution of the block has finished.
-
At file scope, static determines the linkage
of an identifier. When an identifier is declared static it has internal linkage
-
It is not legitimate (or necessary) to declare a function at block scope with storage class static.
extern
-
The usual usage of extern is to declare that an identifier has external linkage
-
It can be legitimate to apply extern when declaring an identifier that has internal linkage
Conflicting declarations
-
It is not legitimate to declare an identifier with both internal and external linkage.
-
The rules by which linkage
is determined are complex, particularly because it is possible to have
more than one declaration of the same object with different storage class
qualifiers. The sequence in which apparently conflicting declarations
occur is significant. The ISO rules say that if the declaration of an
identifier for an object or function contains extern,
the identifier has the same linkage as any visible declaration of the
same identifier with file scope. If the identifier has no visible declaration
with file scope, the identifier has external linkage.
The following two declarations at file scope are compatible.
static int x; /* Internal linkage */
extern int x; /* Same as previous */
But these two declarations are not:
extern int y; /* External linkage */
static int y; /* Internal linkage */
auto
-
The keyword auto is effectively obsolete and its use is not recommended.
register
-
The keyword register is used as an instruction to the compiler to make
any access to an object as fast as possible.
Index