The role of the static keyword in C language

Source: Internet
Author: User

the function of static ( penetrating Analysis )

In C, the literal meaning of static can easily lead us astray, in fact, it has three functions.

( 1 The first and most important one is to introduce it: hide.

When we compile multiple files at the same time, all global variables and functions that do not have a static prefix have global visibility. For the understanding of this sentence, I would say for example. We want to compile two source files at the same time, one is A.C and the other is main.c.

Here's what a.c.

char a = ' a '; Global variable
void msg ()
{
printf ("hello\n");
}

Here's what main.c.

int main (void)
{
extern char A; extern variable must be declared before use
printf ("%c", a);
(void) msg ();
return 0;
}

The running result of the program is:

A Hello

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 ) Static the second function 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.

#include <stdio.h>

int fun (void) {
static int count = 10; In fact, this assignment statement has never been executed.
return count--;
}

int count = 1;

int main (void)
{
printf ("Global\t\tlocal static\n");
for (; Count <=; ++count)
printf ("%d\t\t%d\n", Count, Fun ());

return 0;
}

The running result of the program is:

Global local static

1 10

2 9

3 8

4 7

5 6

6 5

7 4

8 3

9 2

10 1

(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. For example, to initialize a sparse matrix, we can place all the elements in one place 0, and then assign values to elements that are not 0. If it is defined as static, it eliminates the first 0 operation. Another example is to put a character array as a string to use, but also feel each time at the end of the character array to add ' s ' too troublesome. If the string is defined as static, it saves the trouble, because it is already ' the '. Do a little experiment to verify it.

#include <stdio.h>

int A;

int main (void)
{
int i;
static Char str[10];

printf ("Integer:%d; String: (begin)%s (end) ", A, str);

return 0;
}

The running results of the program are as follows

integer:0; String: (Begin) (end)

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.

The role of the static keyword in C language

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.