当前位置:网站首页>Usage of static [detailed explanation]
Usage of static [detailed explanation]
2022-04-22 08:52:00 【Holy meow】

Introduction to this issue
This paper mainly introduces : What is the static,static modification local variable 、 Global variables 、 function Changes that will occur when , What are the essential reasons for these changes ..
Catalog
summary
Static, As the name suggests, it is static 、 Static meaning . stay C In language static Is used to modify variables and functions keyword , Some properties of the modified object will be fundamentally changed , In a sense, these changes seem to fit “ static state ” This means . Then let's talk about it in detail static The usage of this keyword .
usage
static There are three different uses :1. Modify local variables ,2. Modify global variable ,3. Modify function . Here is a detailed explanation and analysis of each usage in turn .
Modify local variables
By static Modified local variables are called : Static variables . Now there's a piece of code , Try to think about what the execution results are :
#include<stdio.h>
void test()
{
int a = 1;
a++;
printf("%d ", a);
}
int main()
{
int i = 0;
while (i < 10)
{
test();
i++;
}
return 0;
}
We know the of local variables Life cycle That's where it is Local scope , To put it simply, enter this range variable to create , If you leave this range, the variable will be destroyed . From the above example, this sentence can also be well confirmed , Every time you call a function test, local variable a create , When the call is over a The destruction , So every print is 2. But if I use static Modify the variables here a, What kind of change will happen ?
#include<stdio.h>
void test()
{
static int a = 1;
a++;
printf("%d ", a);
}
int main()
{
int i = 0;
while (i < 10)
{
test();
i++;
}
return 0;
Why is this so ? Someone might say : because static Modified variables are executed only once in a program , Then it won't change . But I want to ask : Why is that ? Essentially because static The storage location of modified local variables has changed , Local variables are stored in The stack area Inside , Static variables are stored in Static zone Inside . And the impact of this change is to make local variable Of Life cycle Become as long as the whole program , namely : If a local variable is out of its scope, it will not be destroyed , Unless the program ends . It is worth noting that although the declaration cycle of local variables has been greatly improved , But his scope hasn't changed , Or it can only be used in that local scope .
Memory is roughly divided into three areas : The stack area , Heap area , Static zone . Here is just a simple mention , Not deep .
Modify global variable
We know that global variables can be used in different source files , Just need to use extern Make an external statement . for example :
But if use static Decorating this global variable will result in an execution error :
The reason for the error is : Unable to resolve external instruction , This phenomenon occurs The root cause yes : Global variables are variables with External link properties Of , and static When modifying the global variable, put this External link properties Turned into Internal link properties . This makes it impossible for other source files to use the global variable . That's why you feel as if the scope of global variables has become smaller , But the life cycle remains the same , Still the whole project .
Modify function
static Modifier functions are actually similar to global variables , Because functions also have External link properties , By static After modification, it becomes Internal link properties 了 , Other source files can no longer be used .
summary
Local variables are static When decorating, the life cycle will become as long as the whole program , But the scope has not changed . and static When modifying global variables and functions , It can only be used inside the source file , Isolate from the outside .

If this blog is helpful to you , Give bloggers a free like to show encouragement. Welcome to like comment collection ️, thank you !!!
If you have any questions or different opinions , Welcome to the comment area .
版权声明
本文为[Holy meow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220749408521.html
边栏推荐
猜你喜欢

idea中@Data注解,get/set方法不起作用

RHEL 用户和组的管理-笔记

elastic-job安装部署接入

Solve the problem that the disk has space but cannot create files --- repair the server file system

RHEL7 逻辑卷管理-笔记

Const modifier variable

Nessus漏洞扫描简介

goto语句

RHEL7 配置本地yum源

Android Development - SQLite and sqlitedatabase Application Experiment 6 notes
随机推荐
Abbreviation for greater than / less than / equal to (abbreviation of SQL database includes mangodb)
tar 源码包管理-源码包安装方法
详解转义字符
工业缺陷检测项目实战(四)——基于HRNet的陶瓷缺陷检测
sql查询去除空数据和null 字段
冒泡排序及优化
编程中GMT和CST的含义
Fabric测试示例,遇到orderer Exited(x) x seconds
Realization of floor decomposition animation in cesium
Rhel7 configuring local Yum source
Hyperledger Fabric1.4环境搭建及示例测试
PCIe learning - Introduction to PCIe bus architecture: transaction layer - data link layer - physical layer (8)
require-dev.mikey179/vfsStream is invalid, it should not contain uppercase characters. Please use m
CSDN如何转载文章
Restore MySQL service after computer reset
VS编译器注释风格
插入排序及优化
Fabric test example, encountered order exited (x) x seconds
微信小程序手机号码如何进行解密
SQL query removes empty data and null fields







