当前位置:网站首页>Easy to understand C language keyword static
Easy to understand C language keyword static
2022-06-26 01:49:00 【So it is^^】
One :static Modify local variables
Unused static modification

use static After modification

From the code and results in Figure 1, we can see that Not by static modification The first 79 Line code , local variable i In the exit test Function time Will destroy data , Because the first 86 Row to 90 Code loop 10 Secondary entry test() function , Run out test Function is a local variable i Data destruction for , So the result is 10 Time i The value of is 2
As can be seen from Figure 2 , Every time you call test function , The use of i It's all left over from the last function call i, hold The data of local variables is saved , So the results i The value of is 2~11
You can see it here static Modified local variables It will not be destroyed even if it is out of its own scope , Equivalent to changing the life cycle of this local variable
Two :static Modify global variable


LNK In fact, it refers to link error
Figure 1 shows Global variables can be used in other files of the whole project , This is because global variables have external link attributes. As you can see in Figure 2 after static After modification , Global variables can only be used in source files , Other documents cannot be used , It gives us the feeling that the scope has become smaller
We can regard the whole project as a building , Different documents in a project are equivalent to different floors , The global variable is equivalent to a person on a certain floor of that building , In the absence of static When decorating, he can be in that building ( engineering ) Through the stairs ( link ) Can work in different floors ( file ), But once it's been static After modification , It is equivalent to dismantling the stairs ( Does not have external attributes ) A man can only be on his floor ( file ) Work in China
3、 ... and :static Modify function


whole static Decorating functions is similar to decorating global variables , It also makes the original function that can be called in other files of the whole project only in its source file ( That is, where the function is located .c perhaps .h file )
边栏推荐
猜你喜欢

Pixel6 unlock bootloader

What happens from entering a web address in the browser's input box to seeing the contents of the web page?

readv & writev

Viwi interface

轻轻松松理解指针

25. histogram comparison

The 19th - 22nd week of scientific research training - about tnet and memnet

Tengwenze, a hot-blooded boy, was invited to serve as the image ambassador of the global finals of the sixth season perfect children's model

Log4j2 vulnerability

21. Hoff circle transformation
随机推荐
Focal loss
Accumulation of some knowledge points in machine learning
GUN make (4) 规则的命令
Assertion of postman interface test
Simple making of master seal
输入3个整数,从大到小输出出来
OTA trigger
Summary of informer's paper
Exploring temporary information for dynamic network embedding
Postman斷言對應脚本的解釋
王老吉药业“关爱烈日下最可爱的人”公益活动在杭启动
PTA class a simulated seventh bomb: 1160-1163
NLP enhanced technology
正则表达式
17.11 std::atomic续谈、std::async深入谈
GUN make (5) makefile中的变量
Wechat circle of friends test point
PTA class a simulated second bullet: 1136-1139
胰蛋白酶的化学性质及应用
Principle of voice wake-up