C language variables and their effect --static

  In C, static keyword can not only be used to modify variables can also be used to modify the function. When modifying variables using the static keyword, we call this variable as a static variable.

  Storage static variables and global variables are static storage. But here in need of special note is, static variables are static storage, a static variable storage is not necessarily static variables. For example, after the global variable although of a static storage mode, but it is not a static variable, it must be defined by the static to become a static global variable.

The role of the hidden and isolated

  As already explained before, though global variables are static storage, but not static variables. The whole scope of global variables is the source, when a source program by a plurality of source files, a global variable in each source file is valid.

  If we want to be limited to the use of global variables in the source file, the file can not be referenced in other sources, that is to limit its scope only in the definition of the variable within the source file is valid, but not in the other source files in the same source use. At this time, it can be achieved by adding the static keyword before global variables, the global variable is defined as a static global variable. This prevents errors caused in other source files. Also played hide and isolate errors role in the other source files, in favor of modular programming.

Keep the contents of the variable persistence

  Sometimes we want the value of the function of local variables after the end of the function call does not disappear, but still retain their original value. That is, when the memory cell it occupied is not released, the next time the function is called, its local variables still exist, that is, the last value at the end of the function call. At this time, we should use the keyword static local variable is declared as "static local variable."

  When a local variable is declared as static local variable, also changed the storage location of local variables, that is stored from the original stack instead static memory storage. Making it look like a global variable, a static local variable in fact, the main difference is that the global variable visibility, static local variable in the block in which the declaration is only the visible.

  For some subroutine must be maintained in terms of the value of local variables between calls, static local variable is particularly important. If there is no static local variable, you must use global variables in this type of function, which will open the door to the introduction of side-effects. The best example of the use of static local variable is implemented to count the number of functions, as shown in the following example.

#include <stdio.h> 
void COUNT (); 
int main (void) 
{ 
    int I = 0; 
    for (I = 0; I <=. 5; I ++) 
    { 
            COUNT (); 
    } 
    return 0; 
} 
void COUNT () 
{ 
    / * declare a static local variables * / 
    static NUM = 0; 
    NUM ++; 
    the printf ( "% D \ n-", NUM); 
}

  In this code, we passed the count () function in declaring a static local variable num as a counter. Because static local variable is the initial value at compile time, and the initial value only once, it has the initial value when the program runs. After the initial value will not re at each function call, but retain the value at the end of the last function call. In this way, count () function when each is called, will remain static local variable num value on the first call, and then perform the increment operation, thus achieving a counting function. At the same time, it avoids the use of global variables.

By way of example above, we can draw static local variable in general use scenario, as follows:

  • The need to retain the value of the function at the end of the first call.
  • If the initialization variable will only be quoted without changing its value, then use the static local variable is more convenient, so as not to re-assign each call.

The default is initialized to 0

  In the static data area, in memory of all the bytes default value is 0x00. Static variables and global variables, as they are stored in the static data area, and therefore the value of the variable which is also the default 0. Demo example is shown below.

#include <stdio.h>
static int g_x;
int g_y;
int main(void)
{
    static int x;
    printf("g_x:%d\ng_y:%d\nx:%d",g_x,g_y,x);
    return 0;
}

Run results:
g_x: 0
G_Y: 0
the X-: 0  

  

Guess you like

Origin www.cnblogs.com/liang-chen/p/11407844.html