当前位置:网站首页>Three uses of static keyword
Three uses of static keyword
2022-07-27 15:45:00 【FA FA is a silly goose】
stay C In language ,static Keywords are 3 Common usage methods :
1.static Modify local variables
2.static Modify global variable
3.static Modify function
1. When static When modifying a local variable , Let's look at the following code :
int main()
{
for (int i = 0; i < 3; i++)
{
static int a = 0;
a++;
printf("%d ", a);
}
return 0;
}
Output is :
When removing int Ahead static when , The results are as follows :
When there is no static when , The result is 3 individual 1, It's easy for us to understand ,3 Secondary cycle , Every time you enter the cycle ,a The values are 0, So the output of three cycles is 1; When there is static When decorating , Look at the output , Every time you enter the cycle a The value of is at the end of the last cycle a Value , That is, after entering the cycle ,a The value of is not initialized to 0, let me put it another way , By static The modified local variable is out of scope , The life cycle is not over , Until the end of the whole process , The life cycle will end .
2. When static When modifying global variables , Let's look at the following code :

First of all we have test_1.c Create a global variable in this source file a, We will in test.c Make a statement , And then print a Value , The result is 10.
When we use static Modify this global variable , Then print a The value of will be wrong ,
He said a For unresolved external symbols , And we are a The source file to which it belongs ( namely test_1.c) To print in a Value , It can be output normally , So when static When modifying global variables , The modified variable scope is the source file where the variable is located .
3. When static When modifying a function , You can verify it by yourself , Let me tell you the conclusion first ,static When modifying a function , The decorated function can only be used in the source file of the function , This function cannot be called in other source files , This is similar to modifying global variables .
边栏推荐
- HaoChen CAD building 2022 software installation package download and installation tutorial
- 【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
- $router.back(-1)
- 《吐血整理》C#一些常用的帮助类
- C语言:动态内存函数
- Static关键字的三种用法
- Spark 3.0 DPP implementation logic
- Analysis of spark task scheduling exceptions
- What format is this data returned from the background
- 使用Prometheus监控Spark任务
猜你喜欢
随机推荐
线程中死锁的成因及解决方案
flutter —— 布局原理与约束
Spark3中Catalog组件设计和自定义扩展Catalog实现
Text batch replacement function
折半插入排序
Multi table query_ Exercise 1 & Exercise 2 & Exercise 3
Learn parquet file format
Troubleshooting the slow startup of spark local programs
Spark RPC
Database: use the where statement to retrieve (header song)
Is low code the future of development? On low code platform
Spark 3.0 adaptive execution code implementation and data skew optimization
Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
Implement custom spark optimization rules
[正则表达式] 匹配分组
QT (XIII) qchart drawing line chart
[TensorBoard] OSError: [Errno 22] Invalid argument处理
Spark troubleshooting finishing
On juicefs
go语言慢速入门——基本内置类型









