当前位置:网站首页>The use of C language static can flexibly change the life cycle and make you write code like a duck to water
The use of C language static can flexibly change the life cycle and make you write code like a duck to water
2022-07-27 00:29:00 【TheshyO_ o】
stay C In language static Is a very important keyword
static Means static , Its usage is basically divided into three kinds
1. Modify local variables ( Change the life cycle of local variables )
2. Modify global variable ( Make the global variable only act on the current source file )
3. Modify function ( Make the function only work with the current source file )
How to decorate it ? and Life cycle And how it was changed ?
Let me give you an example , Let's take a look at
First write a string to take a The code of the address

Now let's run this code , take a Take a look at the address of

a The address for 00eff748
good , Let's run it again

Will find ,a The address of has become 012ff8b4
a The address of has changed ,a Of Life cycle There is only one cycle
Because a In fact, there is a keyword hidden in front of auto
auto Means automatic , It is an automatic variable , Automatically create , Automatically destroy
If we don't hide our code, it's like this

auto It's generated automatically , Even if you delete it , It's still there
Then how can we change a The life cycle of , Now static That comes in handy
As long as we are int a = 0; prefix static That's all right. , use static hold auto Replace

At this time, we will find that , No matter how many times ,a The address of has changed into one Set value

We can draw a conclusion ,static hold a Of Life cycle Changed the
Of course ,static Not just for addresses , It's in loop The performance is also excellent

You can see , The above two groups of codes , One with static modification , None
There is no code on the left static modification a, therefore a Of Life cycle There is only one cycle , At the end of a cycle, it will be reset to 1
therefore printf The printed result is 2 2 2 2 2 2 2 2 2 2
And the code on the right has a static modification ,a Of Life cycle Be extended , The feature of automatic destruction is cancelled , So in each cycle a The value of +1
therefore printf The printed result is 2 3 4 5 6 7 8 9 10 11
Yes , One more thing to add
static Modify local variables , On the surface, it changes the life cycle of local variables
In essence, it changes the storage type of variables , From stack area to static area
( Memory is basically divided into several areas , The stack area , Heap area , Static zone )

I won't go into details here
stiatic Modify global variable , Change the global variable from acting on the whole project to acting only on the current source file
In essence, it changes the external link attribute of a global variable into an internal link attribute
static Decorate functions with static The essence of modifying global variables is basically the same
Blog for the first time , I have just learned programming 4 God , Please point out any mistakes
Gentlemen, what can you teach me ?
边栏推荐
猜你喜欢

Uni app learning (II)

5_线性回归(Linear Regression)

Web middleware log analysis script 1.0 (shell script)

LeetCode题目——二叉树篇

Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
![[LeetCode] 无重复最长字符串](/img/97/bf8c9b019136ab372ce2c43cddbb2c.jpg)
[LeetCode] 无重复最长字符串

Matlab simulation of inverted pendulum control system based on qlearning reinforcement learning

ResNet论文解读及代码实现(pytorch)

CDs simulation of minimum dominating set based on MATLAB

Configure deeplobcut 1 with your head covered
随机推荐
Pyautogui usage example
The crawler parses the object of the web page. Element name method
Nacos installation and pit stepping
C and pointer Chapter 18 runtime efficiency 18.3 runtime efficiency
Recbole use 1
In JS, the common writing methods and calling methods of functions - conventional writing, anonymous function writing, taking the method as an object, and adding methods to the object in the construct
【 Educational Codeforces Round 132 (Rated for Div. 2) A·B·C】
C and pointers Chapter 18 runtime environment 18.4 summary
蓝桥杯 1004 [递归]母牛的故事
蒙着头配置deeplabcut 1
9_ Logistic regression
Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration
LeetCode——哈希表篇
Matlab simulation of image reconstruction using filtered back projection method
类与对象笔记一
Alexnet (pytoch Implementation)
10_ Evaluate classification
【AtCoder Beginner Contest 261 (A·B·C·D)】
裁剪tif影像
4. Talk about the famous Zhang Zhengyou calibration method