The static keyword

  • 2020-04-01 21:41:16
  • OfStack

(1) let's start with the first and most important one: hiding.

When we compile multiple files at the same time, all global variables and functions that are not prefixed with static are globally visible. To understand this sentence, let me give you an 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 says


char a = 'A'; // global variable
void msg() 
{
    printf("Hellon"); 
}

  Here's what main.c looks like

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

The result of running the program is:

A Hello!

You may ask: why are the global variables a and the function MSG defined in a.c used in main.c? As mentioned earlier, all global variables and functions that are not prefixed with static are globally visible and accessible to other source files. In this example, a is a global variable and MSG is a function, and both are not prefixed with static, so they are visible to the other source main.c.

If you add static, it will be hidden from other source files. For example, if you put static before the definition of a and MSG, main.c will not see them. This feature allows you to define functions and variables with the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables. For functions, Static can only be used to hide, while for variables, Static has the following two functions.

(2) the second function of static is to maintain the persistence of variable contents. Variables stored in static data areas are initialized the first and only time the program starts running. There are two kinds of variables stored in the static storage area: global variables and static variables. However, compared with global variables, static variables can control the visibility of variables. Although this usage is unusual, I'll give you an example.


#include <stdio.h>
int fun(void){
    static int count = 10;    //In fact, this assignment statement was never executed
    return count--;
}
int count = 1;
int main(void)
{    
    printf("globalttlocal staticn");
    for(; count <= 10; ++count)
        printf("%dtt%dn", count, fun());    

    return 0;
}

  The result of running the program is:

Global                  The local static

1                             10

2                             9

3                             8

4                             7

5                             6

6                             5

7                             4

8                             3

9                             2

10                           1

The third function of (3) static is initialized to 0 by default. Global variables also have this property because they are also stored in static data areas. In static data areas, the default value of all bytes in memory is 0x00, which can sometimes reduce the programmer's workload. For example, to initialize a sparse matrix, we can set all the elements to 0 one by one, and then assign several elements that are not 0. If you define it as static, you save the initial zero. Another example is using an array of characters as a string, but finding it troublesome to add '\ 0' to the end of the array each time. If you define the string as static, you save the trouble, because that's just '\0'. You might as well do a little experiment to verify that.


#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 program runs as follows

integer: 0; string: (begin)(end)

Finally, the static of the three effects of a sentence summary. First, the main function of static is hiding, and second, because static variables are stored in static storage, they have persistence and a default value of 0.

The above content is written by Mr. Write of blog garden, which is quite clear and easy to understand.

The following is an essay on zte's 2012 school recruitment pen:

1. What's the difference between a static global variable and a normal global variable?

The description of a global variable (an external variable) is prefixed with static to constitute a static global variable.

Global variables are themselves statically stored, and static global variables are of course statically stored. The two are no different in the way they are stored.

The difference between the two is that the scope of the non-static global variables is the entire source program, and when a source program is composed of multiple source files, the non-static global variables are valid in each source file. A static global variable limits its scope, meaning that it is only valid in the source file where the variable is defined and cannot be used in other source files in the same source program. Since the scope of static global variables is limited to one source file and can only be Shared by functions in that source file, you can avoid causing errors in other source files.

Static global variable is initialized only once to prevent it from being referenced in other file units;

2.   What's the difference between a static local variable and a normal local variable?

  When you change a local variable to a static variable, you change how it's stored and you change its lifetime. Changing a global variable to a static variable changes its scope and limits its use.  

Static local variables are initialized only once, the next time according to the previous result value;

3.   What's the difference between a static function and a normal function?

The static function is different from the normal function scope, only in this file. Functions that are used only in the current source file should be declared as internal (static) functions, which should be declared and defined in the current source file. For functions that can be used outside the current source file, it should be stated in a header file that the source file to use the functions contains.


Related articles: