当前位置:网站首页>通俗易懂C語言關鍵字static
通俗易懂C語言關鍵字static
2022-06-26 01:49: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文件)
边栏推荐
- NLP enhanced technology
- Oracle database complete uninstallation steps (no screenshot)
- Embedded c learning notes
- 2021 - 1 - 15 notes de pêche Ctrl + C / V
- 论文阅读 Exploring Temporal Information for Dynamic Network Embedding
- UN make (6) makefile的条件执行
- Abnova丨CMV CISH 探头解决方案
- Distributed systems (II) understanding of distributed transactions
- Several methods of JQ obtaining objects
- Dataframe extracts data from a column and converts it into a list
猜你喜欢
biggan:large scale gan training for high fidelity natural image synthesis
Principle of voice wake-up
蒟蒻初学单片机的一丢丢笔记
甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人
20. Hough line transformation
Pixel6 unlock bootloader
Obtain WiFi password through computer (only connected WiFi)
浅谈接口测试(二)
Android system startup security
Tengwenze, a hot-blooded boy, was invited to serve as the image ambassador of the global finals of the sixth season perfect children's model
随机推荐
25. histogram comparison
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Hangzhou
论文阅读 Exploring Temporal Information for Dynamic Network Embedding
ActivityManager kill reason
The 19th - 22nd week of scientific research training - about tnet and memnet
2021-1-15 fishing notes ctrl+c /v
Summary of knowledge points of catboost
热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
王老吉药业“关爱烈日下最可爱的人”公益活动在杭启动
recv & send
俏皮少女王艺璇 受邀担任第六季完美童模全球总决赛推广大使
RT thread project engineering construction and configuration - (Env kconfig)
Rollback Protection
输入3个整数,从大到小输出出来
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
Abnova丨ACTN4 DNA 探针解决方案
memory leak check tools 详解
PTA class a simulated third bomb: 1140-1143
23. histogram equalization
浅谈接口测试(一)