Static is the promotion of the Code.
The function of static has the following three articles:
1): Hide.
When compiling multiple files, all global variables and functions that do not have a static prefix have global visibility.
One is A.C and the other is main.c.
Here's what a.c.
<pre lang= "C" escaped= "true" >
char a = ' a '//global variable
void msg ()
{
printf ("hello\n");
}
</pre>
You might ask: Why are global variables A and function msg defined in A.C used in MAIN.C? As mentioned earlier, all global variables and functions that do not have a static prefix have global visibility and other source files can be accessed. In this example, a is a global variable, MSG is a function, and neither has a static prefix, so MAIN.C is visible for additional source files.
If static is added, it is hidden from other source files. For example, before the definition of a and MSG, add STATIC,MAIN.C to see them. With this feature, you can define a function with the same name and a variable of the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables, and for functions, static is limited to hiding, and for variables, static has the following two functions.
2) The second function of static is to keep the contents of the variable 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.
3) The third function of static is initialized to 0 by default. In fact, global variables also have this property, because global variables are also stored in the static data area. In the static data area, all bytes in memory default values are 0x00, sometimes this feature can reduce the workload of the programmer.
Finally, the static three effect of a sentence summary. First, the primary function of static is to hide, and second, because static variables are stored in the static storage area, it has a persistence and default value of 0.
Here's what main.c.
In addition to the header file, you need to declare the function:
<pre lang= "C" escaped= "true" >
void msg ();
int main (void)
{
extern char A; extern variable must be declared before use
printf ("%c", a);
(void) msg ();
return 0;
}
</pre>
Static is the promotion of the Code.