Static is the promotion of the Code.

Source: Internet
Author: User

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.

&nbsp;

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.