This chapter covers in detail the four C storage classes: auto, extern, static, and register. In addition, it includes key sections on program development. The objectives important to this chapter include:
The storage class of a variable determines three things: it controls which functions in a file have access to a variable (which variables are visible), it determines in how many places the same variable can be declared (this is described by linkage), and it determines how long the variable persists in memory.
The visibility of a variable to the various parts of a program is its scope. There are three types of scope: file scope, block scope, and function prototype scope.
There are also three types of linkage: external linkage, internal linkage, and no linkage.
A rule to remember is: Variables with block scope or function prototype scope have no linkage.
C has two storage duration's: static storage duration and automatic storage duration.
Variables declared in a function are automatic by default. An automatic variable has block scope, has no linkage, and has automatic storage duration. An automatic variable is not initialized automatically.
A variable defined outside a function is external. External variables have file scope (one file or all files), external linkage, and static duration. External variables are initialized automatically to zero. If the external variable is defined in another file, declaring the variable with the keyword extern is mandatory.
Static variables have the same scope as automatic variables, except they retain their values and thus have persistent duration. Static variables can be declared inside or outside a function. Defined outside they have external linkage. Defined inside they have internal linkage.
Register variables are the same as automatic variables, except they request the use of a register, if one is available.
Functions can have external (the default) or static storage classes.
The ANSI C library provide the rand() function, designed to return a random number. ANSI C also has a clock() function designed to return the system time.
To reduce a random number to a specific integer range, take the random number modules num (where num represents the range of the random number) and add 1.
Before designing a program, global decisions are required. These decisions include choosing a data form and deciding what information to pass to individual program modules.
There are several advantages of a modular design. Besides making it easier to identify the primary tasks of the program, it is simpler to tinker with the program, to fix a module and leave the rest of the program alone, and to compile a repair program.
The two ANSI C type qualifiers are const and volatile. The const keyword in a declaration establishes a variable whose value cannot be modified by assignment or by incrementing or decrementing.
The volatile keyword in a declaration tells the compiler that a variable can have its value altered by agencies other than the program. This qualifier is used for hardware addresses.