[C language] Usage and detailed explanation of the Static keyword (too detailed!!!)


foreword

  Static is used in different ways in different languages. This article introduces the usage of static in C language .


1. What is static

  static is a keyword in C language, and the Chinese literal translation is "static" . The static keyword is mainly used to modify variables and functions in C language.

Second, the usage of static

1. Modify local variables

  - called static local variables

When static modifies a local variable, it will change the storage location of the local variable , thus making the life cycle of the local variable longer .

  Let's use a piece of code to explain it in detail:

void test()
{
	int x = 0;
	x++;
	printf("%d ", x);
}

int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

  In this code, the local variable x is created and assigned a value of 0 every time the test() function is called, and each time the local variable x exits the test() function, it will destroy itself.

  So it is easy to get the output result as: 1 1 1 1 1 1 1 1 1 1


  Then we use the static keyword to modify the local variable x in the test() function:

void test()
{
	static int x = 0;
	x++;
	printf("%d ", x);
}

int main()
{
	int i = 0;
	while (i < 10)
	{
		test();
		i++;
	}
	return 0;
}

  The output becomes: 1 2 3 4 5 6 7 8 9 10

  The reason is that static modifies the local variable x, making the local variable x static, so that the local variable x will not be destroyed every time the test() function ends, and the original value will be retained when entering the test() function again, so x++ The value is getting bigger and bigger.

Its essence is:

  Ordinary local variables are placed in the stack area after creation . This kind of local variable is created when it enters the scope, and is destroyed when it leaves the scope;

  However, the local variable modified by static is placed in the static area , which changes the storage location of the local variable , thereby prolonging the life cycle of the variable , which is not destroyed until the end of the program.

Note: Static modification of local variables only changes the life cycle, not the scope!


2. Modify global variables

  - called static global variables

  When static modifies a global variable, it will change the linkage attribute of the global variable , thus making the scope of the global variable smaller .

What does this passage mean? Let's briefly talk about global variables   first .

  The scope of global variables is very wide. As long as they are defined in a source file, all source files, objects, and functions in this program can be called, and the life cycle runs through the entire program. When a global variable in a file wants to be used by another file, it needs to be declared externally (declared with the extern keyword below).

  Then we use the code to analyze:

  The global variable x can be used directly in its own file.

  But when the global variable is used directly without external declaration in another file, an undefined error will occur:

   At this point, just use the extern keyword to declare it:

   Then we get to the point, when we modify the global variable x with static :

 

 

  You will find a generation error, the external symbol x cannot be resolved, and the global variable x cannot be called by the test.c file. So we can easily see that the scope of the global variable x is reduced after the static modification, so that the global variable x cannot be called by other files.

Its essence is:

    The global variable itself has an external link attribute , and the global variable defined in the A file can be used in the B file through [ Link ];

    But if the global variable is modified by static, then the external link attribute will be changed to an internal link attribute , and this global variable can only be used in its own source file;

3. Modification function

  - called a static function

  The static modification of a function is very similar to the modification of a global variable. When a function is modified, the link attribute of the function will be changed , thereby making the scope of the function smaller .

  Let's go directly to the code:

  Functions also need to be declared when used in another file.

  But when the function is modified by static :

  You will find that the Add function cannot be parsed, and the scope of the Add function becomes smaller after static modification.

The essence is very similar to global variables:

  The function itself also has an external link attribute ;

  After being modified by static, the external link attribute of the function is changed to an internal link attribute , so that this function can only be used in its own source file, so the scope of the function becomes smaller.


Summarize

  The above is what I want to say. This article only briefly introduces the use of static in C language. There are more ways to use static in other languages. This article is the first knowledge summary of the editor. If there are any deficiencies, you are welcome to point them out in the comment area. I also hope that if you find it useful, one-click three links + follow, thank you!

Guess you like

Origin blog.csdn.net/weixin_60804760/article/details/127467571