当前位置:网站首页>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
边栏推荐
- 在浏览器输入url后执行什么
- HCIA notes
- 【LeetCode】2. Valid parentheses · valid parentheses
- Responsive MySQL of vertx
- Go language foundation ----- 15 ----- reflection
- Go language foundation ----- 19 ----- context usage principle, interface, derived context (the multiplexing of select can be better understood here)
- 【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
- C2 several methods of merging VCF files
- Pat grade a 1029 median
- 技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
猜你喜欢
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
Go language foundation ------17 ----- channel creation, read-write, security shutdown, multiplexing select
OSPF experiment
Collector in ES (percentile / base)
技术干货|AI框架动静态图统一的思考
【LeetCode】2. Valid parentheses · valid parentheses
技术干货|百行代码写BERT,昇思MindSpore能力大赏
Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
Lucene hnsw merge optimization
EtherCAT state machine transition (ESM)
随机推荐
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
Pat grade a 1029 median
PAT甲级 1027 Colors in Mars
UA camouflage, get and post in requests carry parameters to obtain JSON format content
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
Screenshot tool snipaste
【LeetCode】2. Valid Parentheses·有效的括号
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
PAT甲级 1031 Hello World for U
Go language foundation ----- 11 ----- regular expression
Enter three times and guess a number
Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico
一篇文章让你读懂-曼彻斯特编码
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
Pat grade a 1027 colors in Mars
Go language foundation ----- 05 ----- structure
Go language foundation ----- 01 ----- go language features
Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
华为S5700交换机初始化和配置telnet,ssh用户方法
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)