当前位置:网站首页>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 .
边栏推荐
- JS uses unary operators to simplify string to number conversion
- It is said that the US government will issue sales licenses to Huawei to some US enterprises!
- C语言:数据的存储
- QT (IV) mixed development using code and UI files
- go语言慢速入门——go运算符
- 线程中死锁的成因及解决方案
- Modify spark to support remote access to OSS files
- UDP message structure and precautions
- 设置提示框位置随鼠标移动,并解决提示框显示不全的问题
- Using Prometheus to monitor spark tasks
猜你喜欢
![[daily question 1] 558. Intersection of quadtrees](/img/96/16ec3031161a2efdb4ac69b882a681.png)
[daily question 1] 558. Intersection of quadtrees

C语言:数据的存储

/dev/loop1占用100%问题

Dan bin Investment Summit: on the importance of asset management!

网络原理(1)——基础原理概述

【剑指offer】面试题41:数据流中的中位数——大、小堆实现

语音直播系统——提升云存储安全性的必要手段

MLX90640 红外热成像仪测温传感器模块开发笔记(七)
![[TensorBoard] OSError: [Errno 22] Invalid argument处理](/img/bf/c995f487607e3b307a268779ec1e94.png)
[TensorBoard] OSError: [Errno 22] Invalid argument处理

/Dev/loop1 takes up 100% of the problem
随机推荐
Multi table query_ Sub query overview and multi table query_ Sub query situation 1 & situation 2 & situation 3
go语言慢速入门——基本内置类型
Go language slow start - Basic built-in types
JS find the maximum and minimum values in the array (math.max() method)
C语言:扫雷小游戏
Network equipment hard core technology insider router 19 dpdk (IV)
Text batch replacement function
语音直播系统——提升云存储安全性的必要手段
js寻找数组中的最大和最小值(Math.max()方法)
CAS比较交换的知识、ABA问题、锁升级的流程
线程中死锁的成因及解决方案
Read the wheelevent in one article
IP protocol of network layer
Spark Bucket Table Join
Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
QT (XIII) qchart drawing line chart
js操作dom节点
Network equipment hard core technology insider router Chapter 21 reconfigurable router
使用双星号代替Math.pow()
$router.back(-1)