When you normally declare a local variable inside a function, as in the declaration of the variables guess and epsilon in your squareRoot function
float squareRoot (float x)
{
const float epsilon = .00001;
float guess = 1.0;
. . .
}
you are declaring automatic local variables. Recall that the keyword auto can, in fact, pre- cede the declaration of such variables, but is optional because it is the default case. An automatic variable is, in a sense, actually created each time the function is called. In the preceding example, the local variables epsilon and guess are created whenever the squareRoot function is called. As soon as the squareRoot function is finished, these local variables “disappear.” This process happens automatically, hence the name automatic variables.
Automatic local variables can be given initial values, as is done with the values of epsilon and guess, previously. In fact, any valid C expression can be specified as the ini- tial value for a simple automatic variable. The value of the expression is calculated and assigned to the automatic local variable each time the function is called.4 And because an automatic variable disappears after the function completes execution, the value of that variable disappears along with it. In other words, the value an automatic variable has when a function finishes execution is guaranteed not to exist the next time the function is called.
If you place the word static in front of a variable declaration, you are in an entirely new ball game. The word static in C refers not to an electric charge, but rather to the notion of something that has no movement. This is the key to the concept of a static variable—it does not come and go as the function is called and returns. This implies that the value a static variable has upon leaving a function is the same value that variable will have the next time the function is called.
Static variables also differ with respect to their initialization. A static, local variable is initialized only once at the start of overall program execution—and not each time that the function is called. Furthermore, the initial value specified for a static variable must be a simple constant or constant expression. Static variables also have default initial values of zero, unlike automatic variables, which have no default initial value.
In the function auto_static, which is defined as follows:
void auto_static (void)
{
static int staticVar = 100;
.
.
.
}
the value of staticVar is initialized to 100 only once when program execution begins. To set its value to 100 each time the function is executed, an explicit assignment state- ment is needed, as in
void auto_static (void)
{
static int staticVar;
staticVar = 100;
.
.
.
}
Of course, reinitializing staticVar this way defeats the purpose of using a static vari- able in the first place.
Program 8.15 should help make the concepts of automatic and static variables a bit clearer.
Program 8.15 Illustrating Static and Automatic Variables
// Program to illustrate static and automatic variables
#include <stdio.h>
void auto_static (void)
{
int autoVar = 1;
static int staticVar = 1;
printf (“automatic = %i, static = %i\n”, autoVar, staticVar);
++autoVar;
++staticVar;
}
int main (void)
{
int i;
void auto_static (void);
for ( i = 0; i < 5; ++i )
auto_static ();
return 0;
}
Program 8.15 Output
automatic = 1, static = 1
automatic = 1, static = 2
automatic = 1, static = 3
automatic = 1, static = 4
automatic = 1, static = 5
Inside the auto_static function, two local variables are declared. The first variable, called autoVar, is an automatic variable of type int with an initial value of 1. The sec- ond variable, called staticVar, is a static variable, also of type int and also with an ini- tial value of 1. The function calls the printf routine to display the values of these two variables. After this, the variables are each incremented by 1, and execution of the func- tion is then complete.
The main routine sets up a loop to call the auto_static function five times. The output from Program 8.15 points out the difference between the two variable types. The value of the automatic variable is listed as 1 for each line of the display. This is because
its value is set to 1 each time the function is called. On the other hand, the output shows the value of the static variable steadily increasing from 1 through 5. This is because its value is set equal to 1 only once—when program execution begins—and because its value is retained from one function call to the next.
The choice of whether to use a static variable or automatic variable depends upon the intended use of the variable. If you want the variable to retain its value from one function call to the next (for example, consider a function that counts the number of times that it is called), use a static variable. Also, if your function uses a variable whose value is set once and then never changes, you might want to declare the variable static, as it saves the inefficiency of having the variable reinitialized each time that the function is called. This efficiency consideration is even more important when dealing with arrays.
From the other direction, if the value of a local variable must be initialized at the beginning of each function call, an automatic variable seems the logical choice.
Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.