Detailed usage of C language keyword static

Overview

The usage of static can be summarized in three points.
1. Static modifies local variables.
2. Static modifies global variables.
3. The static modification function.
This article summarizes the application of static in C language for reference. Please feel free to correct any errors or omissions.


1, static modifies local variables

Let me show you a program first.

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

The execution result of this code is:
Insert picture description here
Reason: The fun function in the main function is called ten times. Since a is a local variable , a is created when entering the fun function, and a is destroyed when exiting the function. A must be recreated every time the fun function is entered, and so on, 10 results of 2 are printed.


When using static to modify the local variable a variable of the fun function.
void fun()
{
    
    
	static int a = 1;
	a++;
	printf("%d ", a);
}

The printing results at this time are as follows:
Insert picture description here
Reason: static modifies the local variable a, so that a is created once and is no longer destroyed. static changes the life cycle of the local variable a, making its life cycle longer, but does not change its scope. This is the first usage of static.

Significance: 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 will not be misused.

For those who don’t know much about the life cycle of variables, please see this article (scope and life cycle of variables)

2. Static modifies global variables

Insert picture description here
Define global variables in the add.c file and use them in the text.c file. The compilation fails. When declared with extern, the program can run normally:
Insert picture description here
when the
Insert picture description here
global variable is modified with static: the global variable has the attribute of external linking, and other source files can be used through the link. After the global variable is modified by static, it can only be in its own source file. internal use. You cannot link to the source file after it is out. static changes the external link attribute of the global variable to the internal link attribute.

3. Static modified function

The static modified function is similar to the modified global variable. The link attribute of the function is changed, so that the external link attribute of the function becomes the internal link attribute, and the modified function can only be used inside its own source file.
When the function of another .c file is modified with extern, the program runs successfully:
Insert picture description here
When the external function is modified with static:
Insert picture description here

In general:

(1) When static modifies local variables, the static local variables modified by static are initialized only once, and the life cycle of the local variables is prolonged, and they are not released until the end of the program.

(2) When static modifies a global variable, this global variable can only be accessed in this file, not in other files, even if it is an extern external declaration.

(3) If static modifies a function, this function can only be called in this file and cannot be called by other files.

Guess you like

Origin blog.csdn.net/qq_52208569/article/details/112910019
Recommended