当前位置:网站首页>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
边栏推荐
- 华为交换机:配置telnet和ssh、web访问
- 【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
- 什么是定义?什么是声明?它们有何区别?
- Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
- 项目经验分享:基于昇思MindSpore实现手写汉字识别
- Enter three times and guess a number
- Epoll related references
- 微软安全响应中心
- 【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
- Iterm2设置
猜你喜欢

技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
![[mindspire paper presentation] summary of training skills in AAAI long tail problem](/img/34/9c9ec1b94edeecd4a3e7f20fdd8356.png)
[mindspire paper presentation] summary of training skills in AAAI long tail problem

Usage of requests module

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

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

【踩坑系列】mysql 修改root密码失败

在浏览器输入url后执行什么

技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型

UA camouflage, get and post in requests carry parameters to obtain JSON format content

HarmonyOS第三次培训笔记
随机推荐
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
Pat class a 1028 list sorting
lucene scorer
【LeetCode】2. Valid Parentheses·有效的括号
Implementation of breadth first in aggregation in ES
The babbage industrial policy forum
Go language foundation ----- 02 ----- basic data types and operators
What did the DFS phase do
华为交换机配置ssh登录远程管理交换机
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
[at] abc 258G - Triangle 三元组可达-暴力
[coppeliasim4.3] C calls UR5 in the remoteapi control scenario
【踩坑系列】mysql 修改root密码失败
Go language foundation ----- 13 ----- file
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
Chapter VI - Containers
华为交换机基础配置(telnet/ssh登录)
Mail sending of vertx
Grpc message sending of vertx
一篇文章让你读懂-曼彻斯特编码