18.1.5: Storage Classes

A full-blown declaration in C consists of several parts: the storage class, the base type, any type qualifiers, and a list of declarators, where each declarator consists of: an identifier, additional characters possibly indicating that it is a pointer, array, or function, and finally an optional initializer. We've met each of these parts at various points along the way, although we have never explicitly mentioned or defined the storage class. The storage class is optional; it generally appears at the beginning of the declaration (before the base type) if it appears at all. At most one storage class may appear in any one declaration.

We've seen two storage classes so far, extern and static. extern marks a declaration as an external declaration, indicating that the identifier declared has its defining instance somewhere else (where ``somewhere else'' might be somewhere else in the same source file, or in a different source file). static is used in two different ways, (1) to indicate that a global (``file scope'') variable is private to one source file, and cannot be accessed (even with external declarations) from other source files, or (2) to indicate that a local variable should have static duration, such that it does not come and go as the function is called and returns, and so that its value persists between invocations of the function.

Besides these two, there are three other storage classes. register indicates that the programmer believes that the variable will be heavily used, and that it should be assigned to a high-speed CPU register (rather than an ordinary memory location) if possible. Explicit register declarations are rare these days, because modern compilers generally do an excellent job, all by themselves and without any hints, of deciding which variables belong in machine registers. A limitation of register variables is that you cannot generate pointers to them using the & operator. (This is because, on most machines, pointers are implemented as memory addresses, and CPU registers usually do not have memory addresses.)

The fourth storage class is auto, which indicates that a local variable should have automatic duration. (Automatic duration, remember, means that variables are automatically allocated when a function is called and automatically deallocated when it returns.) Since automatic duration is the default for local variables anyway, auto is virtually never used; it's a relic from C's past.

The fifth storage class, typedef, is described in the next section.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback