当前位置:网站首页>static的作用
static的作用
2022-07-05 07:33:00 【luoganttcc】
C 语言中 static 的作用
分类 编程技术
在 C 语言中,static 的字面意思很容易把我们导入歧途,其实它的作用有三条。
(1)先来介绍它的第一条也是最重要的一条:隐藏。
当我们同时编译多个文件时,所有未加 static 前缀的全局变量和函数都具有全局可见性。为理解这句话,我举例来说明。我们要同时编译两个源文件,一个是 a.c,另一个是 main.c。
下面是 a.c 的内容:
代码在git
a.c 文件代码
char a = 'A'; // global variable
void msg()
{
printf("Hello\n");
}
下面是 main.c 的内容:
main.c 文件代码
int main(void)
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
(void)msg();
return 0;
}
程序的运行结果是:
A Hello
你可能会问:为什么在 a.c 中定义的全局变量 a 和函数 msg 能在 main.c 中使用?前面说过,所有未加 static 前缀的全局变量和函数都具有全局可见性,其它的源文件也能访问。此例中,a 是全局变量,msg 是函数,并且都没有加 static 前缀,因此对于另外的源文件 main.c 是可见的。
如果加了 static,就会对其它源文件隐藏。例如在 a 和 msg 的定义前加上 static,main.c 就看不到它们了。利用这一特性可以在不同的文件中定义同名函数和同名变量,而不必担心命名冲突。static 可以用作函数和变量的前缀,对于函数来讲,static 的作用仅限于隐藏,而对于变量,static 还有下面两个作用。
(2)static 的第二个作用是保持变量内容的持久。
存储在静态数据区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化。共有两种变量存储在静态存储区:全局变量和 static 变量,只不过和全局变量比起来,static 可以控制变量的可见范围,说到底 static 还是用来隐藏的。虽然这种用法不常见,但我还是举一个例子。
#include <stdio.h>
int fun(void){
static int count = 10; // 事实上此赋值语句从来没有执行过
return count--;
}
int count = 1;
int main(void)
{
printf("global\t\tlocal static\n");
for(; count <= 10; ++count)
printf("%d\t\t%d\n", count, fun());
return 0;
}
程序的运行结果是:
global local static
1 10
2 9
3 8
4 7
5 6
6 5
7 4
8 3
9 2
10 1
(3)static的第三个作用是默认初始化为0。其实全局变量也具备这一属性,因为全局变量也存储在静态数据区。在静态数据区,内存中所有的字节默认值都是0x00,某些时候这一特点可以减少程序员的工作量。比如初始化一个稀疏矩阵,我们可以一个一个地把所有元素都置0,然后把不是0的几个元素赋值。如果定义成静态的,就省去了一开始置0的操作。再比如要把一个字符数组当字符串来用,但又觉得每次在字符数组末尾加’\0’太麻烦。如果把字符串定义成静态的,就省去了这个麻烦,因为那里本来就是’\0’。不妨做个小实验验证一下。
#include <stdio.h>
int a;
int main(void)
{
int i;
static char str[10];
printf("integer: %d; string: (begin)%s(end)", a, str);
return 0;
}
程序的运行结果如下
integer: 0; string: (begin)(end)
最后对static的三条作用做一句话总结。首先static的最主要功能是隐藏,其次因为static变量存放在静态存储区,所以它具备持久性和默认值0。
边栏推荐
- [tf1] save and load parameters
- The number of occurrences of numbers in the offer 56 array (XOR)
- Efficiency difference: the add method used by the set directly and the add method used by the set after judgment
- Process (P) runs, and idle is different from pycharm
- Professional knowledge of public security -- teacher bilitong
- Ggplot2 drawing learning notes in R
- 目标检测系列——Faster R-CNN原理详解
- Readme, self study record
- 2022 PMP project management examination agile knowledge points (7)
- Install deeptools in CONDA mode
猜你喜欢

CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)

Today, share the wonderful and beautiful theme of idea + website address

QT small case "addition calculator"

I implement queue with C I

2022年PMP项目管理考试敏捷知识点(7)

并查集理论讲解和代码实现

Self summary of college life - freshman

SD_ CMD_ RECEIVE_ SHIFT_ REGISTER

Daily Practice:Codeforces Round #794 (Div. 2)(A~D)

大学生活的自我总结-大一
随机推荐
The number of occurrences of numbers in the offer 56 array (XOR)
Set theory of Discrete Mathematics (I)
Chapter 2: try to implement a simple bean container
Typescript get timestamp
[vscode] prohibit the pylance plug-in from automatically adding import
CADD course learning (5) -- Construction of chemosynthesis structure with known target (ChemDraw)
M2dgr slam data set of multi-source and multi scene ground robot
The SQL implementation has multiple records with the same ID, and the latest one is taken
Target detection series - detailed explanation of the principle of fast r-cnn
[untitled]
Import CV2, prompt importerror: libcblas so. 3: cannot open shared object file: No such file or directory
Today, share the wonderful and beautiful theme of idea + website address
[neo4j] common operations of neo4j cypher and py2neo
DelayQueue延迟队列的使用和场景
Basic series of SHEL script (I) variables
Idea shortcut key
Ue5 hot update - remote server automatic download and version detection (simplehotupdate)
ImportError: No module named ‘Tkinter‘
String alignment method, self use, synthesis, newrlcjust
deepin 20 kivy unable to get a window, abort