当前位置:网站首页>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 :
3
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 ~
边栏推荐
- Compound RateModel合约解析
- 模线性方程组(中国剩余定理+通用解法)
- 二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
- 排序——归并排序
- 零基础自学SQL课程 | UNION 联合查询
- Uoj 551 [unr 4] campus stroll [good polynomial questions (FOG)]
- 2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
- 【HDU6357】Hills And Valleys(DP)
- [noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
- [atcoder2306] rearranging (topology)
猜你喜欢

【IoT】项目管理:如何打造更好的跨职能团队?
![[atcoder1980] mystious light (mathematical simulation)](/img/c0/7de31b36e11ff71328d927c1d1c2d3.png)
[atcoder1980] mystious light (mathematical simulation)

QObject usage skills -- control function class

C wechat upload form data
![20200803 T3 my friends [divide and conquer NTT optimization recursion]](/img/35/01201e3136e3dd5cd562a0481f1ee9.jpg)
20200803 T3 my friends [divide and conquer NTT optimization recursion]

Import on CSDN MD file
![[Oracle database] mammy tutorial day02 use of database management tool sqlplus](/img/f2/8f6f74a62427ebfb4c805c1e9b3352.png)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus

C language function stack frame

C memory alignment

JVM tuning
随机推荐
Simple use of string
二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...
Modular linear equations (Chinese remainder theorem + general solution)
[cluster] haproxy load balancing
Arduino_ STM development record
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
【AtCoder2376】Black and White Tree(博弈)
C- print 99 multiplication table
Pat class A by category
[atcoder1980] mystious light (mathematical simulation)
【集群】haproxy负载均衡
20200730 T3 small B farm [maximum perimeter empty rectangle (monotone stack + line segment tree)] & "ROI 2017 day 2" learning track
C+tinycthread implementation thread
Aiop introduction
Sort - Swap sort
[atcoder1984] wide swap
排序——交换排序
Compound RateModel合約解析
What exactly is PMP?
【AtCoder2307】Tree Game(博弈)