当前位置:网站首页>Static关键字的三种用法
Static关键字的三种用法
2022-07-27 14:27:00 【发发是只呆头鹅】
在C语言中,static关键字有3种常见的使用方法:
1.static修饰局部变量
2.static修饰全局变量
3.static修饰函数
1.当static修饰局部变量时,我们来看下面的代码:
int main()
{
for (int i = 0; i < 3; i++)
{
static int a = 0;
a++;
printf("%d ", a);
}
return 0;
}
输出为:
当去掉int前面的static时,我们得到的结果如下:
当没有static时,结果为3个1,我们很容易理解,3次循环,每次进入循环,a的值都为0,所以三次循环的输出都为1;当有static修饰时,从输出结果来看,每次进入循环a的值是上一次循环结束时a的值,也就是进入循环后,a的值没有被初始化为0,换句话说,被static修饰的局部变量出了作用域,生命周期也没有结束,直到整个程序结束,生命周期才会结束。
2.当static修饰全局变量时,我们来看下面的代码:

首先我们在test_1.c这个源文件中创建一个全局变量a,我们再在test.c声明一下,然后打印a的值,结果为10。
当我们用static去修饰这个全局变量,再去打印a的值会发生错误,
他说a为无法解析的外部符号,而我们在a所属的那个源文件(即test_1.c)中去打印a的值,可以正常输出的,所以当static修饰全局变量时,被修饰的变量作用域为变量所在的源文件。
3.当static修饰函数时,大家可以自行去验证,我先把结论告诉大家,static修饰函数时,被修饰的函数只能在该函数的源文件中使用,在其他源文件中不能调用该函数,这一点和修饰全局变量很相似。
边栏推荐
- Leetcode-1737- minimum number of characters to change if one of the three conditions is met
- 使用解构交换两个变量的值
- 聊聊面试必问的索引
- Troubleshooting the slow startup of spark local programs
- How to edit a framework resource file separately
- Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
- With just two modifications, apple gave styleganv2 3D generation capabilities
- Jump to the specified position when video continues playing
- [daily question 1] 558. Intersection of quadtrees
- $router.back(-1)
猜你喜欢

Leetcode 783. binary search tree node minimum distance tree /easy

Adaptation verification new occupation is coming! Huayun data participated in the preparation of the national vocational skill standard for information system adaptation verifiers

How to edit a framework resource file separately

IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items

实体类(VO,DO,DTO)的划分

Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry

复杂度分析

C语言:动态内存函数

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

What format is this data returned from the background
随机推荐
Network equipment hard core technology insider router Chapter 11 Cisco asr9900 disassembly (V)
C:什么是函数中的返回值(转)
Photoelectric isolation circuit design scheme (six photoelectric isolation circuit diagrams based on optocoupler and ad210an)
Network equipment hard core technology insider router Chapter 17 dpdk and its prequel (II)
Complexity analysis
Overview of wechat public platform development
Unity's simplest object pool implementation
C语言:动态内存函数
The first common node of the two linked lists of "Jianzhi offer"
Explanation of various attributes of "router link"
Four kinds of relay schemes driven by single chip microcomputer
Network equipment hard core technology insider router Chapter 18 dpdk and its prequel (III)
shell脚本读取文本中的redis命令批量插入redis
[正则表达式] 匹配分组
TCC
Unity performance optimization ----- drawcall
初探JuiceFS
How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
QT (IV) mixed development using code and UI files
【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针