[C language] usage of static

Table of contents

static modified local variables

Static modified global variables

static modifier function

Summarize


static modified local variables

Static modified local variables can also be called static local variables

In order to introduce static, let's first analyze the running results of the following code:

#include<stdio.h>
void test()
{
    int a = 5;
	a++;
	printf("%d ", a);
}
int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

There is a test function in the code, and a local variable a is defined in the test function; every time the test function is called, a space will be allocated to a in memory, the initial value of a is 5, and then a is incremented by 1. When the test function call ends, the variable a will be destroyed, and the memory space for a will be re-allocated the next time the test function is called. And the test function is called 10 times in total, so 10 6s appear on the screen

If you add the keyword static in front of a, what will happen? Is the answer still 10 6s?

#include<stdio.h>
void test()
{
    static int a = 5;
	a++;
	printf("%d ", a);
}
int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

 We found that the result of this appearance is different from the above: a seems to have an additive effect

 The following analysis principle:

When learning C/C++ code, roughly divide the memory into three areas: stack area, static area, and heap area

 The variable modified by static is a static variable. We already know from the above figure that the variable created in the static area will not be destroyed until the end of the program, so every time when the test function ends, the variable a is not destroyed. Every time test is called function, the value of a is the value at the end of the previous call.

It can be seen from this that when a local variable is modified by static, a local variable originally exists in the stack area, and it exists in the static area after being modified by static. Static modification of the local variable changes the storage type (location) of the variable and finally makes the life of the static variable The period becomes longer, but the scope remains the same.

 The difference between static modified local variables and global variables: static modified local variables can only be used in the local scope.

Static modified global variables

Static modified global variables can also be called static global variables

When we create multiple files under a project, as shown below

 At this point the program can run normally:

 However, when we modify the global variable g_val with static, the program will report an error:

  The reason why the error occurs is because of the addition of static.

Static modifies the global variable, changing the link attribute of the global variable from an external link attribute to an internal link attribute. This static variable can only be used inside the source file where it is located, and cannot be used inside other source files. (It feels like the scope is getting smaller)

static modifier function

Static modified functions are also called static functions

The following is a project created two files, add.c file and test2.c file

 At this point, the compiled program runs normally

However, when we modify Add with static, an error occurs in the program, and the error message is the same as the static modification of the global variable:

Static modified functions are the same as static modified global variables

The function itself has an external link attribute, but it becomes an internal link attribute when it is modified by static, so that this function can only be used inside the source file where it is located, and cannot be used inside other files.

Summarize

static modified local variables

        Static modification of local variables changes the life cycle of variables so that static local variables still exist outside the scope, and the life cycle ends when the program ends.

Static modified global variables

        A global variable is modified by static, so that this global variable can only be used in this source file, and cannot be used in other source files.

static modifier function

        A function is modified by static, so that this function can only be used in this source file, and cannot be used in other source files.


 

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/130161171