1: Hide
If a project exists A.C, B.C and MAIN.C and other C files.
. static Functions
The function is defined as a static function by adding the keyword staticbefore the return type of the function.
(The function name in the A.C file is static, which can only be called in A.C, not by B.C or MAIN.C)
the definition and declaration of a function is extern by default, but the static function is only visible in the file that declares him and cannot be used by other files.
. Local static variables
The local variable is defined as a local static variable by adding the keyword staticbefore the local variable.
1) in-memory location: Static Storage Area
2) Initialize: Uninitialized global static variable is automatically initialized to 0by the program (the value of the automatic object is arbitrary unless he is displayed initialized)
3) Scope: The scope is still a local scope, and when the function or statement block defining it ends, the scope ends.
The second function of 2:static is to keep the variable content persistent.
Variables stored in the static data area are initialized at the start of the program and are the only one initialized. A total of two variables are stored in static storage: Global variables and static variables, except that, compared to global variables, static can control the visible range of variables, in the final analysis, static is used to hide them. Although this usage is not common, I would like to cite an example.
1#include <stdio.h>2 3 intFunvoid){4 Static intCount =Ten;//in fact, this assignment statement has never been executed.5 returncount--;6 }7 8 intCount =1;9 Ten intMainvoid) One { Aprintf"global\t\tlocal static\n"); - for(; Count <=Ten; ++count) -printf"%d\t\t%d\n", Count, Fun ()); the - return 0; -}
if the static int, Count = 10, is executed as a normal statement, the count is initialized to 10 each time the fun function is called, and it is clear that this is not the case. So this assignment is written there, but it's not actually in the code-compiled machine instructions. Since the assignment statement is not executed, why is its initial value 10? In fact, during the compilation phase, the value of the static storage allocated for this variable in the executable is assigned 10, and the program reads 10 directly into the memory when it is loaded, without the "assignment" action.
(3)The third function of static is initialized to 0 by default
The role of the static keyword