当前位置:网站首页>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 .
边栏推荐
- Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
- Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
- HaoChen CAD building 2022 software installation package download and installation tutorial
- 【剑指offer】面试题53-Ⅱ:0~n-1中缺失的数字——二分查找
- C语言:字符串函数与内存函数
- Summer Challenge harmonyos realizes a hand-painted board
- 使用Prometheus监控Spark任务
- Implement custom spark optimization rules
- Singles cup, web:web check in
- 【剑指offer】面试题54:二叉搜索树的第k大节点
猜你喜欢
随机推荐
Use double stars instead of math.pow()
Read the wheelevent in one article
Analysis of spark task scheduling exceptions
Text batch replacement function
Spark 3.0 DPP实现逻辑
QT (five) meta object properties
Network device hard core technology insider router Chapter 15 from deer by device to router (Part 2)
Singles cup, web:web check in
网络层的IP协议
网络原理(1)——基础原理概述
Summer Challenge harmonyos realizes a hand-painted board
Go language slow start -- go operator
使用解构交换两个变量的值
STL value string learning
go语言慢速入门——go运算符
Pictures to be delivered
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
leetcode-1:两数之和
js使用一元运算符简化字符串转数字
C语言:函数栈帧









