Summary of three usages of static keyword static in C language

Time:2021-8-28

Different from other keywords, they have a variety of uses, and they can be used in a certain environment to improve the running performance of the program and optimize the structure of the program. This article mainly introduces the function of static keyword static in C language, which is very helpful for everyone to learn C language.

 

Static keyword static

In C language, static keyword modifies variables and functions:

1. Local variable

2. Global variable

3. Functions

The basic usage of static keyword is:

1. The variables modified by static belong to class variables, which can be directly referenced by class name and variable name, without the need to create a new class

2. The method modified by static belongs to class method, which can be directly referenced by class name and method name without new class

Variables modified by static and methods modified by static belong to the static resources of the class, which are shared between class instances. In other words, they change everywhere. JDK puts different static resources in different classes instead of putting all static resources in one class. Many people may take it for granted that they should do so, but have you ever thought about why? I think there are three main advantages:

1. Different classes have their own static resources, which can realize the classification of static resources. For example, static resources related to mathematics are placed in java.lang.math, and static resources related to calendar are placed in java.util.calendar, which is very clear

2. Avoid duplicate names. It is also normal for different classes to have duplicate static variable names and static method names. If all of them are put together, an inevitable problem is that the names are repeated. What should we do at this time? Just sort it out.

3. It is easy to understand to avoid the infinite expansion of static resource classes.

 

Let’s learn about the usage of static keyword.

Modify local variables

1. A local variable modified with the static keyword static will open up space for the variable in the data area and initialize it during compilation. If it is not initialized in the code, the system will initialize it to 0 by default.

2. A local variable modified with static will prolong the life of the local variable and exceed the life of the function.

3. Initialization of local variables decorated with static keywords.

Take the following two variables A and B as examples. During compilation, it is found that variables A and B are static variables, and variables A and B will be identified. When the program runs, allocate space for variables A and B before calling the main function, and initialize variable A. since variable B is initialized by the parameters passed by function fun, Therefore, variable B will be initialized when the program calls the function. Before initializing variable B, the identification bit of variable B will be checked. If the identification bit is 0, it will be initialized with function parameter X. if the identification bit is 1, it will not be initialized.

static int a = 0;( (initialization during compilation)

static int b = x;( Initialize when running this code)

void fun(int x)

{

static int a = 10;

static int b = x;

a++;

b++;

}

Modify global variables

Global variables are defined outside the function body. Storage space is allocated in the global data area, and the compiler will initialize them automatically.

Common global variables are visible to the whole project. Other files can be directly used after external declaration. In other words, other files can no longer define a variable with the same name (otherwise the compiler will think they are the same variable).

Static global variables are only visible to the current file. Other files cannot be accessed. Other files can define variables with the same name. The two do not affect each other.

When defining global variables that do not need to be shared with other files, adding the static keyword can effectively reduce the coupling between program modules, avoid the conflict of variables with the same name in different files, and will not be misused.

Modifier function

The use of a function is similar to that of a global variable. Adding static before the return type of a function is a static function. Its characteristics are as follows:

A static function can only be visible in the file that declares it. Other files cannot reference the function

Different files can use static functions with the same name without affecting each other

Non static functions can be referenced directly in another file without even having to use an extern declaration

The following two files illustrate that functions declared with static cannot be referenced by another file:

/* file1.c */

#include

static void fun(void)

{

    printf(“hello from fun.\n”);

}

int main(void)

{

    fun();

    fun1();

    return 0;

}

/* file2.c */

#include

static void fun1(void)

{

    printf(“hello from static fun1.\n”);

}

use   When compiling GCC file1. C File2. C, the error report is as follows:

/TMP / cc2vmzgr. O: in function ‘main’:

static_ Fun. C: (. Text + 0x20): undefined reference to ‘fun1’

collect2: error: ld returned 1 exit status

Modify the file without using the static modifier. You can refer to this function in another file:

/* file1.c */

#include

void fun(void)

{

    printf(“hello from fun.\n”);

}

/* file2.c */

int main(void)

{

    fun();

    return 0;

}

Same use   GCC file1. C File2. C is compiled and passed. The running results are as follows:

-> % ./a.out

hello from fun.

summary

Static is a very useful keyword. If used properly, it can add icing on the cake to the program. Of course, some company coding specifications clearly stipulate that all functions only used in this document should be declared with the static keyword, which is a good coding style.

In any case, pay attention to your coding habits in actual coding, and try to reflect the elegance of the language itself and the coding quality of the coder.

The above is the full usage of the keyword static. I hope it will be helpful for you to use the keyword static flexibly.

 

If you want to better improve your programming ability, learn C language and C + + programming well! Overtake at the corner, take a quick step!
C language c + + Learning Circle】, sharing (source code, project actual combat video, project notes, basic introductory tutorial)
Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning books:

 

Programming learning video: