当前位置:网站首页>About static keyword

About static keyword

2022-06-11 07:43:00 Fried tomatoes 110

Study c Language for a few days , Here to tell you about the key words static The role of ~

static stay c Language can be used to modify local variable Global variables as well as function . The use static To modify their respective functions , Now let's take a look at .

1、 Modify local variables

Let's take a look at this code first :

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

Running results :

It's simple , Obviously the result of the run is the output 10 individual 1. But if you use static Keyword to decorate print A local variable in a function a Well ( The code is as follows ), The results are 10 individual 1 Do you ? 

 

Running results :

??? Why not 10 individual 1 What about it  , That's the keyword static It worked .

Before explaining , We need to know how the memory is allocated when the code is compiled . Memory is a relatively large storage space , When in use, it will be divided into different functional areas , It is mainly divided into three parts , The stack area 、 Heap area and static area . See the following figure for the storage of variables :

  Local variables are placed in the stack area , But with static After modification Changed the storage type of the variable , From the original stack storage to Static storage area , Thus, the local variables in the static area will not be destroyed when they leave the active area , Equivalent to changing the life cycle of local variables . So every time the above code calls printf Function time , What is used a Is the previous call print Function a. So the final output is 1 2 3 4 5 6 7 8 9 10.

2、 Modify global variable

Let's take a look at it first static To modify the global variables of external files ~

Running results :

Everyone should be able to think of this . But with static To modify a What will happen , Please look at the chart below. :

  Compiler error , Why is that ?

It turns out that a global variable can be used by other files in the whole project because it has an external link attribute , And when a global variable is static After modification , Its external link attribute becomes an internal link attribute , Therefore, this global variable can only be used in its own source file , Other files can no longer be used .

ststic Modify global variable , This makes the external link attribute of the global variable become the internal link attribute , This makes its scope smaller , But the life cycle has not changed ~

3、 Modify function

Let's see if it's useless static What happens when you decorate a function ?

Running results :

  I'm smart

Then if you use static To modify print What about functions ?

Let's see

Compiler error , Why? ?

Because functions also have external link properties in a project , But with static After modification, its external link attribute becomes an internal link attribute , So that it can only be used inside its own source file , Cannot be used in other source files , It gives us the feeling that the scope of action is small ~

原网站

版权声明
本文为[Fried tomatoes 110]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020519106082.html