Storage in C



Bibliographic Information


Intimately bound with the idea of scope is that of storage. When a program defines a variable, the compiler will decide how to store the value of that variable, and create a space in memory for the value. The compiler uses the declaration to determine how much space to allocate and how to allocate it—as temporary storage (such as on a stack) or as more permanent storage (in data space.)

Recall that the format of a C variable declaration is: storage-class type identifier. The part of this declaration relevant to the storage is, not surprisingly, storage-class. The storage classes are static, auto, register, and extern. (There is a fifth keyword, typedef, that for syntactic reasons is considered a storage class; but it plays no part in anything that follows.)

In each of the following sections we shall discuss one type of storage declaration, its effects, and when it is legal. In the last section, we shall look at a simple program written in a variety of ways and show how the results of changing storage classes affects the way the compiler handles storage.

Copyright Notice

©1990 by Matt Bishop. The definitive version was published in The C Journal 8(5), May 1990.