Three usages of static keyword in C language

Introduction to scope and life cycle
Static modification of local variables
Static modification of all variables
Static modification of

function scope and life cycle Introduction
What kind of variables are called local variables?
Just look at the picture >>, where b and c are local variables, and only a is a global variable.

insert image description here

scope

Scope (scope) is a programming concept. Generally speaking, the name used in a piece of program code is not always valid/available,
and the code scope that limits the availability of this name is the scope of this name.

insert image description here

Summarize

The scope of a local variable is the local scope in which the variable resides.
The scope of global variables is the entire project.

life cycle

The life cycle of a variable refers to the time period between the creation of the variable and the destruction of the variable.
insert image description here
We can try to print a, b and c respectively in the main function to see what kind of results will be
insert image description here
insert image description here
insert image description here
seen, a, and c can print the result, but b reports an error, which is the result of their different scope and life cycle, and also the difference between global variables and local variables.

Summarize

The life cycle of a local variable is: the life cycle of entering the scope begins, and the life cycle of the out of scope ends. (Plainly speaking, it is destroyed when it leaves the scope, and the variable will be recreated when it enters the function.) The
life cycle of global variables is: the life cycle of the entire program.

static modified local variables

The purpose of introducing the scope and life cycle is to better understand the keyword of C language - static.
Next, I will explain to you the magical effect of static modification of local variables>

#include<stdio.h>

void  txit()
{
    
    
	 int b= 2 ;
	 b = b + 1;
	 printf("%d ", b);
	 
}
int main()
{
    
    
	for (int i = 0; i < 3; i++)
	{
    
    
		txit();
	}
	return 0;
}

insert image description here
The static modified local variable will change its life cycle, that is, extend its life cycle, so that when the function is called, the b variable is not destroyed, and the first time we do not have static modification, the function call is over, b The variable is then destroyed and recreated when re-entering the function, so the method in Figure 1 is always 3, and Figure 2 will not be destroyed after the function is called, but retain this value. When will it be destroyed? The answer is that it will be destroyed after the program is executed.

Summarize

1. Static modified local variables have a longer life cycle

Static modifies all variables

Since statically modified local variables extend the life cycle, does static modification of global variables also extend the life cycle?
The answer is no, because the life cycle of global variables is already destroyed after the program is executed, which is equivalent to the longest life cycle.
insert image description here
We can see that the program can run normally in this situation.
At this time, we use static to modify the global variable a to see what is going on.
insert image description here
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. It can also be understood that its life cycle is shortened

Summarize

Static modified global variables, so that this global variable can only be used in this source file, and cannot be used in other source files
. life cycle shortened

static modifier function

insert image description here
Here are also two different files, use extern to declare the function.
We see that the summation is now running normally.
Next, we need to decorate the function with static >

insert image description here
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. This situation is similar to static modification of global variables

Summarize

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/fjj2397194209/article/details/131346094