当前位置:网站首页>Static keyword
Static keyword
2022-07-03 07:45:00 【It's Beichen not too PI acridine】
static keyword
- Modifying variables
- Modify function
- static The third function of is default initialization to 0
- static What's the difference between global variables and ordinary global variables ?
- static What's the difference between local variables and ordinary local variables ?
- static What is the difference between ordinary functions and ordinary functions ?
This keyword is in C There are two main functions in language ,C++ It is extended .
Modifying variables
The first function : Modifying variables . Variables are divided into local and global variables , But they all exist in static areas of memory .
Static global variables
Scope is limited to the file in which the variable is defined , Other documents are used extern The statement can't use him . To be precise, scope starts from where it is defined , End at the end of the file , It cannot be used in the first lines of code where it is defined . If you want to use it, you have to add extern , Want to be easy to use , It's simple , Just define it directly at the top of the file .
Static local variables
Defined in the function body , It can only be used in this function , Other functions in the same file cannot be used . Because of being static Modified variables always exist in the static area of memory , So even if this function ends , The value of this static variable will not be destroyed , This value can still be used next time the function is used .
i and j What are the values of , Why? ?
static int j;
void fun1(void)
{
static int i = 0;
i++;
}
void fun2(void)
{
j = 0;
j++;
}
int main()
{
for(k=0; k<10; k++)
{
fun1();
fun2();
}
return 0;
}
i The value of is 10.i Is a local static variable , Store in local static area , Its value will not be cleared as the function call ends , Life cycle still exists , When the function is called again , Its value is the value after the last call .
j The value of is 1.j Is a static global variable , A global variable originally has an external link attribute , But be static After modification , The external link attribute becomes the internal link attribute , You can only use it inside your own source file , Cannot be used inside other files . It has no effect on the variables used later in the definition of this document , stay for Cyclic invocation fun2() when ,fun2() It will be assigned as 0.
Modify function
The second function : Modify function . Add... Before the function static Make the function static . But here, “static” It's not about storage , It means that the scope of the function is limited to this file ( So it is also called internal function ). The advantage of using intrinsic functions is : When different people write different functions , Don't worry about your own functions , Whether it will have the same name as the function in other files .
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
static What's the difference between global variables and ordinary global variables ?
Global variables ( External variables ) Before the description of static It's a static global variable .
Global variables themselves are static storage , Static global variables are also static storage . There is no difference between the two in the way of storage .
Changing a global variable to a static variable changes its scope , It limits its use .
The difference between the two is that the scope of non static global variables is the whole source program , When a source program consists of multiple source files , Non static global variables are valid in all source files . Static global variables limit their scope , That is, it is only valid in the source file where the variable is defined , It cannot be used in other source files of the same source program . Because the scope of static global variables is limited to one source file , Can only be used for functions within the source file , So you can avoid causing errors in other source files .
static Global variables are initialized only once , Prevent references in other file units ;
static What's the difference between local variables and ordinary local variables ?
Changing a local variable to a static variable changes its storage mode, that is, its lifetime .
static Local variables are initialized only once , Next time according to the last result value ;
static What is the difference between ordinary functions and ordinary functions ?
But here, “static” It's not about storage , It means that the scope of the function is limited to this file ( So it is also called internal function ).static Functions are different in scope from ordinary functions , Only in this document . Functions used only in the current source file should be described as internal functions (static Modified function ), Internal functions should be described and defined in the current source file .
For functions that can be used outside of the current source file , It should be stated in a header file , The source file to use these functions should contain this header file .
summary : In fact, the most important thing is to distinguish the scope and storage mode
边栏推荐
- Research shows that breast cancer cells are more likely to enter the blood when patients sleep
- Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
- static关键字
- OSPF experiment
- Traversal in Lucene
- Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
- Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
- HCIA notes
- 哪一刻你才发现青春结束了
- Go language foundation ----- 04 ----- closure, array slice, map, package
猜你喜欢

研究显示乳腺癌细胞更容易在患者睡觉时进入血液

Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select

Go language foundation ----- 06 ----- anonymous fields, fields with the same name

技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验

技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!

Go language foundation ----- 02 ----- basic data types and operators

项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass

Go language foundation ----- 11 ----- regular expression

Pat class a 1028 list sorting

【踩坑系列】mysql 修改root密码失败
随机推荐
C2 several methods of merging VCF files
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
lucene scorer
Structure of golang
Responsive MySQL of vertx
Sent by mqtt client server of vertx
HCIA notes
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
【LeetCode】2. Valid parentheses · valid parentheses
PDO and SDO concepts
技术干货|昇思MindSpore创新模型EPP-MVSNet-高精高效的三维重建
【LeetCode】4. Best Time to Buy and Sell Stock·股票买卖最佳时机
图像识别与检测--笔记
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
Industrial resilience
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
Iterm2设置
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select