C language 丨 three usage summary of static keyword static

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

 

Static keyword static

In C language, the static keyword modifies variables and functions:

1. Local variables

2. Global variables

3. Function

The most basic usage of the static keyword is:

1. 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 the class method, which can be directly referenced by the class name. The method name does not need to create a 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, one change and everywhere change. 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 this is necessary, but have you ever wondered why you want to do this? Personally think there are three main benefits:

1. Different classes have their own static resources, which can realize static resource classification. For example, static resources related to mathematics are placed in java.lang.Math, and static resources related to calendars are placed in java.util.Calendar, so it is very clear

2. Avoid duplicate names. It is normal for different classes to have static variable names and static method names with the same name. If all of them are put together, the inevitable problem is duplication of names. What should I do? Just place it in categories.

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

 

Let's take a look at the usage of the static keyword in detail.

Modification of local variables

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

2. Local variables modified with static will extend the life of the local variables and exceed the life of the function.

3. Initialization of local variables modified by static keywords.

The following two variables a and variable b are explained. During the compilation process, it is found that variables a and b are static variables, which will identify variables a and b. When the program is running, before the main function is called, the variables a and b are Variable b allocates space and initializes variable a. Since variable b is initialized by the parameter passed by function fun, variable b will be initialized when the program calls the function. Before variable b is initialized, variable b will be checked. The flag bit, if the flag bit is 0, it will be initialized with the function parameter x, and the flag bit is 1 will not be initialized.

static int a = 0; (initialization during compilation)

static int b = x; (initialize when running the 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 automatically initialize them.

Common global variables are visible to the entire project, and other files can be used directly after being declared externally by extern. In other words, other files can no longer define a variable with the same name (otherwise the compiler will consider them the same variable).

Static global variables are only visible to the current file, other files are inaccessible, other files can define variables with the same name, and 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 conflicts of variables with the same name in different files, and prevent misuse.

Decorated function

The use of functions is similar to global variables. Add static before the return type of the function, which is a static function. Its characteristics are as follows:

A static function can only be visible in the file where it is declared, and 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 directly referenced in another file, even without extern declaration

The following two file examples show that functions declared using static cannot be referenced by another file:

/* file1.c */

#include <stdio.h>

static void fun(void)

{

    printf("hello from fun.\n");

}

int main(void)

{

    fun();

    fun1();

    return 0;

}

/ * file2.c * /

#include <stdio.h>

static void fun1(void)

{

    printf("hello from static fun1.\n");

}

When compiling with gcc file1.c file2.c, the error report is as follows:

/tmp/cc2VMzGR.o: In the function'main':

static_fun.c:(.text+0x20): undefined reference to'fun1'

collect2: error: ld returned 1 exit status

Modify the file, do not use the static modifier, you can reference the function in another file:

/* file1.c */

#include <stdio.h>

void fun(void)

{

    printf("hello from fun.\n");

}

/ * file2.c * /

int main(void)

{

    fun();

    return 0;

}

Also use gcc file1.c file2.c to compile, compile and pass, the running result is as follows:

-> % ./a.out

hello from fun.

to sum up

Static is a very useful keyword, used properly can make the program icing on the cake. Of course, some company coding standards clearly stipulate that all functions that are only used in this file must be declared with the static keyword. This is a good coding style.

In any case, you should pay attention to your coding habits when actually coding, and try to reflect the elegance of the language and the coding quality of the coder.

The above is all the usage of the keyword static, I hope it will be helpful for everyone to use the keyword static flexibly.

 

If you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster!
[ C language C++ learning penguin circle ], share (source code, project actual combat video, project notes, basic introductory tutorial)
welcome partners who change careers and learn programming, use more information to learn and grow faster than you think!

Programming learning books:

 

Programming learning video:

 

Guess you like

Origin blog.csdn.net/Hsuesh/article/details/112605370