当前位置:网站首页>C语言静态变量static
C语言静态变量static
2022-07-31 05:17:00 【冷漠原则百分百】
对C语言静态变量static分析,解释了static为什么在局部变量里面定义的时候不会随着函数执行完毕而销毁,也稍微用解释了静态存储区和动态存储区的小知识
学习static前要先认识
- 了解什么是静态存储区和动态存储区。

- 静态存储区就是图中的:代码区(text代码区) 、全局区(bss段,data段,文字常量区)
- 动态存储区就是栈和堆
阅读下面的文章前,对于初学者要记住一个概念:全局区是在静态存储区的。
在c语言编译四个阶段,写好代码然后点击编译运行—>预编译–>编译—>汇编—>链接。在编译链接的阶段,就决定了把谁放在bbs段,谁放在data段。
在定义全局变量时,程序员没有人工初始化的,都初始化为0然后只能放在全局区的全局未初始化bss段,有初始化值的放在全局区的全局初始化数据data段。(字符串常量这些就肯定放在全局区里面的文字常量区了的,不是放在data段)。
在定义局部变量时候,分为auto,static,register三种类型,auto是普通的,我们经常写的这种,就放在栈区,调用执行完到就销毁了。register是存放在寄存器中的
static分析
全局/局部变量 和 static静态变量 之间存在的关系
在全局变量前面加一个static时,该变量就变成了全局静态变量,在局部变量前面加一个static时,改变量就变成了局部静态变量
在局部变量中添加static的分析
int main(){
static int a;
void add(){
static int i = 10;
i++;
printf("%d\n",i);
}
add();//11
add(); //12
return 0;
}
由此可见 在局部变量中,用static包裹的变量,就相当于把变量i放入了静态存储区中。可相当于一个全局变量来用了。
总结:当static加在局部变量时
- 延长了生命周期,一直到整个程序结束,才结束
- 变量存放在静态变量区,不初始化时候,会默认初始为0
在全局变量添加static的分析
本来就是全局变量了,加个static有什么用呢?可见这当然不是为了延长生命周期了。
//a.h文件
static int a;
int b = 10;
#include <stdio.h>
#include "a.h"//引入自定义的头文件
extern int a;
extern int b;
//b.c文件
int main(){
printf("%d\n",a);//没有
printf("%d\n",b);//10
return 0;
}
总结:当static加在了全局变量
- 不可以被其他文件访问,起了一个隐藏的作用
- 不会与其他文件重复名字的变量起冲突
- 提高了数据的安全性
边栏推荐
猜你喜欢

DingTalk Enterprise Internal-H5 Micro Application Development

Cholesterol-PEG-Azide CLS-PEG-N3 Cholesterol-PEG-Azide MW:3400

拒绝采样小记

化学试剂磷脂-聚乙二醇-氨基,DSPE-PEG-amine,CAS:474922-26-4
![[Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config](/img/53/8b5a12e7ed551dca52ada5dbb5d6b5.png)
[Solved] ssh connection report: Bad owner or permissions on C:\\Users/XXX/.ssh/config

Learning and understanding of ROS service programming

After unicloud is released, the applet prompts that the connection to the local debugging service failed. Please check whether the client and the host are under the same local area network.

DSPE-PEG-Biotin,CAS:385437-57-0,磷脂-聚乙二醇-生物素可延长循环半衰期

mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids

Tensorflow steps on the pit while using it
随机推荐
MW: 3400 4-Arm PEG-DSPE four-arm-polyethylene glycol-phospholipid a saturated 18-carbon phospholipid
mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
Redis-哈希
虚拟机查看端口号进程
CAS:474922-22-0 Maleimide-PEG-DSPE Phospholipid-Polyethylene Glycol-Maleimide Brief Description
box-shadow相关属性
Where can I find the private files set by myself?
ROS之service传输图片
Jupyter内核正忙、内核挂掉
unicloud cloud development record
mPEG-DSPE 178744-28-0 甲氧基-聚乙二醇-磷脂酰乙醇胺线性PEG磷脂
【Rhapsody学习笔记】4:Relations
Tensorflow steps on the pit while using it
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
这些数组技巧,我爱了
ROS之service编程的学习和理解
Cholesterol-PEG-NHS NHS-PEG-CLS cholesterol-polyethylene glycol-active ester can modify small molecular materials
Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
MySQL master-slave switching steps
DSPE-PEG-COOH CAS:1403744-37-5 磷脂-聚乙二醇-羧基脂质PEG共轭物