About the use of static

About the use of static

1, local variables:

  First, local variables, any function defined in the interior of a variable (including main) belong to this category. At run time, the compiler does not initialize local variables, that is the initial value of the local variable is random, unless its explicit assignment. Local variables are stored in the program stack space will be released immediately after use;

  But for static local variable, the arrival of a modified static, even if it is not explicitly assigned, the compiler will put it to zero. And static local variables are stored in the global data area, even after the function returns, the variables are not cleared;

  Static local variable = global variables, in favor of a modular program.

2, global variables:

  Global variables in the program itself are stored in the global data area, the compiler is initialized to 0;

  , Ordinary global variables can be used in c language works in different programs, only extern external declaration, but other files can not have the same name with its variable;

  The static global variable is visible only to the current file, another file is inaccessible.

3, function

  For functions, the return of the front type becomes a static plus static function, similar to the specific use of global variables;

  Non- static function can be called across files.

to sum up:

  static role:

    1, into a static variable, stored in the global data area, to prevent all kinds of bug caused due to failure to initialize;

    2, cross-file to prevent static global variables, static functions caused by the same name bug (c language);

Guess you like

Origin www.cnblogs.com/-hhs/p/11025579.html