当前位置:网站首页>通俗易懂C语言关键字static
通俗易懂C语言关键字static
2022-06-26 00:13:00 【原来是这样^^】
一:static修饰局部变量
未用static修饰

用static修饰后

从图一的代码和结果可以看出在未被static修饰第79行代码时,局部变量i在退出test函数时会销毁数据,因为第86行到90代码循环10次进入test()函数,运行完test函数时局部变量i的数据销毁,因此结果出现了10次 i的值为2
从图二可以看出,每一次调用test函数,使用的i都是上一次函数调用留下的i,把局部变量的数据保存下来了,因此结果i的值为2~11
从这里可以看出static修饰的局部变量出了自己的作用域也不会被销毁,相当于改变了这个局部变量的生命周期
二:static修饰全局变量


LNK 其实是指链接错误
图一可以看出用全局变量在整个工程的其他文件内部能被使用,是因为全局变量具有外部链接属性图二可以看到经过static修饰后,全局变量只能在源文件使用,其它文件不能使用,给我们的感觉相当于作用域变小了
我们可以把整个工程看作成一栋楼,一个工程里面不同的文件相当于不同的楼层,全局变量相当于一个人在那栋楼的某一层,在未被static修饰时他可以在那栋楼(工程)通过楼梯(链接)可以在不同的楼层中干活(文件),但是一旦被static修饰后,就相当于把楼梯拆了(不具有外部属性)人只能在他所在的楼层(文件)中工作
三:static修饰函数


全static修饰函数和修饰全局变量差不多,也是使得原本函数可以在整个工程的其它文件调用变得只能在他的源文件使用(也就是函数所在的.c或者.h文件)
边栏推荐
- Summary of informer's paper
- Dataframe converts the data type of a column
- 2022 Anhui province safety officer C certificate examination practice questions simulated examination platform operation
- Operation of simulated examination platform for electrical test questions in 2022
- Redis的使用
- 23. histogram equalization
- CityJSON
- 热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
- 秀场精灵陈梓桐 受邀担任第六季完美童模全球总决赛首席体验官
- GNN (graph neural network) introduction vernacular
猜你喜欢

Log4j2 vulnerability

pixel 6 root

biggan:large scale gan training for high fidelity natural image synthesis

Abnova丨CSV 单克隆抗体解决方案

How to add a "security lock" to the mobile office of government and enterprises?

readv & writev

如何为政企移动办公加上一道“安全锁”?

Energetic girl wangyujie was invited to be the spokesperson for the global finals of the sixth season perfect children's model

biggan:large scale gan training for high fidelity natural image synthesis

26. histogram back projection
随机推荐
PTA class a simulated sixth bomb: 1156-1159
丨EGFR FISH 探针解决方案
热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
Redis7.0的安装步骤
wifi 的理论速度计算方法
easyexcel读取文件
浅谈接口测试(二)
JSON instance (I)
2021-1-15 摸魚做的筆記Ctrl+c /v來的
17.11 std::atomic续谈、std::async深入谈
pixel 6 root
Redis的使用
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Hangzhou
Viwi interface
makefile 中export set ENV 的区别和作用
User unlock status query
Test questions and answers for the 2022 baby sitter (Level 5) examination
Focal loss
[visual studio code] vscode shortcut keys