Previous: 14.1.5 An Example: Lexical Scanner
Up: 14.1 Storage Classes
Next: 14.1.7 Storage Class for Functions
Previous Page: 14.1.5 An Example: Lexical Scanner
Next Page: 14.1.7 Storage Class for Functions

## 14.1.6 Static Variables

As we have seen, external variables have global scope across the entire program (provided extern declarations are used is files other than where the variable is defined), and a lifetime over the the entire program run. The storage class, static, similarly provides a lifetime over the entire program, however; it provides a way to limit the scope of such variables, Static storage class is declared with the keyword static as the class specifier when the variable is defined. These variables are automatically initialized to zero upon memory allocation just as external variables are. Static storage class can be specified for automatic as well as external variables.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus, the value of a static variable in a function is retained between repeated function calls to the same function. The scope of static automatic variables is identical to that of automatic variables, i.e. it is local to the block in which it is defined; however, the storage allocated becomes permanent for the duration of the program. Static variables may be initialized in their declarations; however, the initializers must be constant expressions, and initialization is done only once at compile time when memory is allocated for the static variable.

Figure 14.9 shows an example which sums integers, using static variables. Function sumit() reads a new integer and keeps a cumulative sum of the previous value of the sum and the new integer read in. The cumulative value of sum is kept in the static variable, sum. The driver, main() calls sumit() five times to sum five integers.

Sample Session:

• ***Static Variables***
• Please enter 5 numbers to be summed
• Enter a number: 12
• The current total is 12
• Enter a number: 23
• The current total is 35
• Enter a number: 34
• The current total is 69
• Enter a number: 45
• The current total is 114
• Enter a number: 56
• The current total is 170
• Program completed

While the static variable, sum, would be automatically initialized to zero, it is better to do so explicitly. In any case, the initialization is performed only once at the time of memory allocation by the compiler. The variable sum retains its value during program execution. Each time the function sumit() is called, sum is incremented by the next integer read.

Static storage class designation can also be applied to external variables. The only difference is that static external variables can be accessed as external variables only in the file in which they are defined. No other source file can access static external variables that are defined in another file.

```/* File: xxx.c */
static int count;
static char name[8];
main()
{
... /* program body */
}```

Only the code in the file xxx.c can access the external variables count and name. Other files cannot access them, even with extern declarations.

We have seen that external variables should be used with care, and access to them should not be available indiscriminately. Defining external variables to be static provides an additional control on which functions can access them. For example, in the symbol.c example in the last section, we created a file symio.c which contained an external variable. This external variable should be accessible only to the functions in that file. However, there is no way to guarantee that some other file may not access it by declaring it as extern. We can ensure that this will not happen by declaring the variable as static as shown in Figure 14.10.

The static variable c would not be accessible to functions defined in any other file, thus preventing an unplanned use of it as an external variable by the code in other files.

Previous: 14.1.5 An Example: Lexical Scanner
Up: 14.1 Storage Classes
Next: 14.1.7 Storage Class for Functions
Previous Page: 14.1.5 An Example: Lexical Scanner
Next Page: 14.1.7 Storage Class for Functions

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:21:51 HST 1994