当前位置:网站首页>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 ~
边栏推荐
- mpi
- Simple use of string
- 【集群】haproxy负载均衡
- 【AtCoder1984】Wide Swap (拓扑排序转化)
- [Oracle database] mammy tutorial day04 Sorting Query
- QT table display data
- What is the lifecycle of automated testing?
- Classification of MNIST datasets with keras
- Classes and objects (Part 2)
- [noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
猜你喜欢
随机推荐
What is the lifecycle of automated testing?
C language three chess games
Matrix tree theorem
[cluster] haproxy load balancing
Aiop introduction
[IOT] project management: how to build a better cross functional team?
【HDU6357】Hills And Valleys(DP)
运筹学导论
QObject usage skills -- control function class
Introduction to operations research
Implementation of queue (C language)
排序——选择排序
零基础自学SQL课程 | OUTER JOIN外连接
.NET C#基础(6):命名空间 - 有名字的作用域
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
20200803 T3 my friends [divide and conquer NTT optimization recursion]
Miscellany C language
【AtCoder2306】Rearranging(拓扑)
Zero foundation self-study SQL course | outer join external connection
Flask页面的分页




![[codeforces1019e] raining season](/img/8e/4a96954ee7dae5f81eaae05b5a075b.png)


![[untitled] Weng_ C lesson 1](/img/4e/41876093ef6b6a38909832f89e1495.jpg)
