当前位置:网站首页>通俗易懂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文件)
边栏推荐
- Dataframe converts the data type of a column
- Is it safe to log in the stock account on the flush? How to open a stock account in the flush
- 热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
- Common basic Oracle commands
- 元气少女王钰洁 受邀担任第六季完美童模全球总决赛代言人
- 17.11 std::atomic续谈、std::async深入谈
- Data analysis - similarities and differences between C-end and b-end data analysis
- 蒟蒻初学单片机的一丢丢笔记
- GNN (graph neural network) introduction vernacular
- Obtain WiFi password through computer (only connected WiFi)
猜你喜欢

CityJSON

GNN (graph neural network) introduction vernacular

RT thread project engineering construction and configuration - (Env kconfig)

Embedded c learning notes

tos cos dscp 区别和作用

Redis7.0的安装步骤

Can bus transceiver principle

清甜女孩李斯霞 受邀担任第六季完美童模全球总决赛小主持人

Android system startup security

The 19th - 22nd week of scientific research training - about tnet and memnet
随机推荐
PTA class a simulated second bullet: 1136-1139
元素定位的八大法则
25. histogram comparison
PTA class a simulated 11th bomb: 1124-1131
Xiaomi tablet 5 Pro unlock bootloader
Operation of simulated examination platform for electrical test questions in 2022
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
GNN (graph neural network) introduction vernacular
Oracle database complete uninstallation steps (no screenshot)
How to add a "security lock" to the mobile office of government and enterprises?
JQ获取对象的几种方式
热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人
Installing MySQL databases in FreeBSD
PTA class a simulated ninth bullet: 1114-1117
Summary of xlnet model
User unlock status query
浅谈接口测试(一)
Obtain WiFi password through computer (only connected WiFi)
Is it safe to log in the stock account on the flush? How to open a stock account in the flush