当前位置:网站首页>Function of static
Function of static
2022-07-05 07:35:00 【luoganttcc】
C In language static The role of
classification Programming technology
stay C In language ,static It's easy to lead us astray , In fact, it has three functions .
(1) Let's introduce the first and most important one : hide .
When we compile multiple files at the same time , All without static Global variables and functions of prefixes have global visibility . To understand this sentence , Let me give an example . We need to compile two source files at the same time , One is a.c, The other is main.c.
Here is a.c The content of :
Code in git
a.c File code
char a = 'A'; // global variable
void msg()
{
printf("Hello\n");
}
Here is main.c The content of :
main.c File code
int main(void)
{
extern char a; // extern variable must be declared before use
printf("%c ", a);
(void)msg();
return 0;
}
The result of the program running is :
A Hello
You may ask : Why is it a.c Global variables defined in a And the function msg Can be in main.c Use in ? As I said before , All without static Global variables and functions of prefixes have global visibility , Other source files can also be accessed . In this case ,a Global variable ,msg Is the function , And they didn't add static Prefix , So for another source file main.c Is visible .
If you add static, Will hide from other source files . For example, in a and msg Add before the definition of static,main.c You can't see them . With this feature, functions and variables with the same name can be defined in different files , You don't have to worry about naming conflicts .static Can be used as a prefix for functions and variables , For functions ,static The role of is limited to hiding , And for variables ,static There are also the following two functions .
(2)static The second function of is to keep the variable content persistent .
Variables stored in the static data area are initialized as soon as the program starts to run , It's also the only initialization . There are two types of variables stored in static storage : Global variables and static Variable , Just compared to global variables ,static You can control the visible range of variables , At the end of the day static Or for hiding . Although this usage is not common , But let me give an example .
#include <stdio.h>
int fun(void){
static int count = 10; // In fact, this assignment statement has never been executed
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;
}
The result of the program running is :
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 The third function of is default initialization to 0. In fact, global variables also have this property , Because global variables are also stored in the static data area . In the static data area , The default value of all bytes in memory is 0x00, Sometimes this feature can reduce the workload of programmers . For example, initialize a sparse matrix , We can set all the elements one by one 0, And then put it not 0 Assign values to several elements of . If defined as static , It eliminates the need to start with 0 The operation of . Another example is to use a character array as a string , But I think every time I add... At the end of the character array ’\0’ too troublesome . If you define a string as static , It saves the trouble , Because it was ’\0’. You might as well do a small experiment to verify .
#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;
}
The program runs as follows
integer: 0; string: (begin)(end)
Finally, static Make a one sentence summary of the three functions of . First static The main function of is to hide , Second, because static Variables are stored in static storage , So it has persistence and default values 0.
边栏推荐
- CADD course learning (6) -- obtain the existing virtual compound library (drugbank, zinc)
- 使用go语言读取txt文件写入excel中
- C learning notes
- ModuleNotFoundError: No module named ‘picamera‘
- Apple script
- CADD课程学习(5)-- 构建靶点已知的化合结构(ChemDraw)
- Delayqueue usage and scenarios of delay queue
- Play with grpc - go deep into concepts and principles
- Selenium element positioning
- The problem of configuring opencv in qt5.13.2 is solved in detail
猜你喜欢
Matrix and TMB package version issues in R
HDU1231 最大连续子序列(分治or动规or双指针)
Microservice registry Nacos introduction
Machine learning Seaborn visualization
借助 Navicat for MySQL 软件 把 不同或者相同数据库链接中的某数据库表数据 复制到 另一个数据库表中
Close of office 365 reading
Ugnx12.0 initialization crash, initialization error (-15)
Play with grpc - go deep into concepts and principles
Rough notes of C language (2) -- constants
And play the little chestnut of dynamic agent
随机推荐
Idea to view the source code of jar package and some shortcut keys (necessary for reading the source code)
The number of occurrences of numbers in the offer 56 array (XOR)
剑指 Offer 56 数组中数字出现的次数(异或)
Apple system shortcut key usage
What is soda?
list. files: List the Files in a Directory/Folder
Shadowless cloud desktop - online computer
[neo4j] common operations of neo4j cypher and py2neo
R language learning notes 1
Ue5 hot update - remote server automatic download and version detection (simplehotupdate)
Exit of pyGame, idle and pycharm
Line test -- data analysis -- FB -- teacher Gao Zhao
What does soda ash do?
【idea】Could not autowire. No beans of xxx type found
2022年PMP项目管理考试敏捷知识点(7)
611. Number of effective triangles
DataGrid offline installation of database driver
CADD课程学习(6)-- 获得已有的虚拟化合物库(Drugbank、ZINC)
P3D gauge size problem
Professional knowledge of public security -- teacher bilitong