Third, static variables
The type descriptor for a static variable is static. Static variables are, of course, static storage, but the amount of static storage is not necessarily static variables, such as external variables are static storage mode, but not necessarily static variables, must be defined by static to become static external variables, or static global variables. For an automatic variable, it is previously described as a dynamic storage method. But you can also use static to define it as a static automatic variable, or a static local variable, thus becoming a statically stored mode.
In this view, a variable can be explained by static and change its original storage mode.
1. Static local Variables
A static local variable is formed by adding the static descriptor before the description of the local variable.
For example:
static int a,b;
static float array[5]={1,2,3,4,5};
Static local variables, which are static storage, have the following characteristics:
(1) Static local variables are defined within functions, but not as automatic variables, which exist when called, and disappear when the function exits. Static local variables always exist, meaning that its lifetime is the entire source program.
(2) The lifetime of a static local variable is the entire source program, but the scope is still the same as the automatic variable, that is, it can only be used within the function that defines the variable. After exiting the function, it cannot be used, although it continues to exist.
(3) allows the initial value to be assigned to the static local amount of the constructed class. In the array chapter, the description of array initialization is described. If the initial value is not assigned, the system automatically assigns a value of 0.
(4) The system automatically assigns a 0 value to a static local variable of a basic type if it is not assigned an initial value at the time of the description. For an automatic variable, the value is indeterminate. According to the characteristics of static local variables, it can be seen that it is a lifetime of the entire source program. Although it cannot be used after leaving the function that defines it, it can continue to work when the function that defines it is invoked again, and the value left after the previous call is saved. Therefore, you can consider static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although the above goal can be achieved with global variables, global variables sometimes cause unexpected side effects, so it is advisable to adopt local static variables.
[Example 5.15]main ()
{
int i;
void f (); /* Function Description */
for (i=1;i<=5;i++)
F ();/* Function call */
}
void F ()/* Function definition */
{
Auto int j=0;
++j;
printf ("%d\n", j); The function f is defined in the
}
program, where the variable j is described as an automatic variable and given an initial value of 0. When F is called more than once in Main, J is assigned an initial value of 0, so each output is 1. Now change J to a static local variable, and the program is as follows:
Main ()
{
int i;
void f ();
for (i=1;i<=5;i++)
F ();
}
void F ()
{
static int j=0;
++j;
printf ("%d\n", j);
}
void F ()
{
static int j=0;
++j;
printf ("%d/n", j);
}
Because J is a static variable that can retain its value after each call and continue to use the next time it is called, the output value becomes an additive result. The reader can analyze its execution process on its own.
2. Static global variables
The description of the global variable (external variable) before being labeled static constitutes a static global variable. The global variable itself is the static storage mode, static global variables are of course also static storage mode. The two are not different in the way they are stored. The difference between the two is that the scope of the Non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can only be common to functions within that source file, so it is possible to avoid causing errors in other source files. From the above analysis, we can see that the change of the local variable to the static variable is the change of its storage mode that changes its lifetime. Changing the global variable to a static variable changes its scope and limits its
Scope of use. So the static this specifier plays a different role in different places. Should be taken into consideration.
Four, register variable
All of these variables are stored in memory, so when you read and write to a variable frequently, you have to repeatedly access the internal memory, which can cost a lot of access time. For this reason, the C language provides another variable, the register variable. This variable is stored in the CPU registers, when used, does not need to access memory, and directly from the register read and write, so as to improve efficiency. The descriptor for the register variable is the register. The cyclic control variables with more cycles and the repeated variables in the circulation body can be defined as register variables.
[Example 5.16] Find ∑200i=1imain ()
{
Register i,s=0;
for (i=1;i<=200;i++)
S=s+i;
printf ("s=%d\n", s);
}
This program cycles 200 times, I and S will be used frequently, so it can be defined as a register variable.
The following points are also described for register variables:
1. Only local automatic variables and formal parameters can be defined as register variables. Because the register variable belongs to the dynamic storage mode. The amount of data that needs to be stored in a static way cannot be defined as a register variable.
2. In the C language used by computers such as Turbo C,ms C, the register variable is actually treated as an automatic variable. So speed does not improve. Register variables are allowed in the program only to be consistent with standard C. 3. Even machines that actually use register variables, because the number of registers in the CPU is limited, the number of register variables is limited.