当前位置:网站首页>Keywords static, extern + global and local variables
Keywords static, extern + global and local variables
2022-04-23 20:47:00 【Computer white】
Catalog
One 、 A brief introduction to global and local variables
Two 、 keyword + Detailed explanation of global and local variables
1. keyword static+ Modify local variables
2. keyword extern+ Modify global variable
Preface
C All variables in a language have their own scope .C Variables in language , There are two types of... By scope , Local variables and global variables . Before learning global and local variables , First of all, we need to understand the keywords static and extern( Next I will introduce in detail )
Once in the program, it's like a sea , Since then, long hair is a passer-by ......
One 、 A brief introduction to global and local variables
null , stay {} The variables defined inside are local variables , stay {} All variables defined outside are global variables
#include <stdio.h>
int b = 10; //b Defined as a global variable
int main()
{
int i = 0;//i Defined as a global variable
return 0;
}
Two 、 keyword + Detailed explanation of global and local variables
stay C In language , Keywords are C Language gives us , You can't create !!!
static Is used to modify variables and functions :
1. Modify local variables - Called static local variables
2. Modify global variable - Called static global variables
3. Modify function - Called static function
1. keyword static+ Modify local variables
The following code example :
// Code 1( Without any modification )
#include <stdio.h>
void test()
{
static int i=0;
i++;
printf("%d ", i);
}
int main()
{
int i = 0;
for(i=0; i<10; i++)
{
test();
}
return 0;
}
Code example after running ( Yes no static Modify and contrast ):
From the above we can see that , When the code has no static When decorating , The result of running is different ...
The first code diagram runs parsing ( nothing static When decorating ): We all know that code execution is compiled and executed from top to bottom , Compile to for loop , That is, enter the custom function test(),test() Is an independent piece of program code , When a piece of program code is executed , Our data will be destroyed , Not in storage , This led to the second visit test() when ,i The value of is still equal to 0; So the running structure of the program will have a loop , Always for 1.
The first code diagram runs parsing ( Yes static When decorating ): When there is static When decorating ,static Modifying a local variable will change the life cycle of the variable ,test() in i The value of is out of order test() This program code will not be destroyed ,i The value of will gradually increase , Static variables are out of scope and still exist , Until the end of the program , The life cycle is over
2. keyword extern+ Modify global variable
The following code example :
// Code 1
//test1.c( Create another source file )
int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
// Code 2
//test1.c( Create another source file )
static int g_val = 2018;
//test.c
int main()
{
printf("%d\n", g_val);
return 0;
}
Code example after running ( nothing static modification ):
Code example after running ( Yes static modification ):
From the above we can see that , When the code has no static When decorating , The result of running is different ...
The first code diagram runs parsing ( nothing static When decorating ): From the above, we can know that a global variable has external link properties , Can be used under different source files , You need keywords when using extern Statement can be used only
The second code diagram runs parsing ( Yes static When decorating ): A global variable is originally defined by an external link attribute , By static After modification , It becomes an internal link property , Can only be used in their own source file , Cannot be used in other files , In use, the scope becomes smaller , So there will be unresolved external symbols .
extern yes C Language keywords , Is used to declare external symbols .
If a project consists of multiple source files , If you want to refer to an external variable defined in another source file in one source file , You need to use... In the source file of the referenced variable extern Keyword declaration variables .
3.static Modify function
// Code 1
//add.c
int Add(int x, int y)
{
return x+y;
}
//test.c
extern int Add(int x,int y);// Declare external symbols
int main()
{
printf("%d\n", Add(2, 3));
return 0;
}
// Code 2
//add.c
static int Add(int x, int y)
{
return x + y;
}
//test.c
extern int Add(int x,int y);// Declare external symbols
int main()
{
printf("%d\n", Add(2, 3));
return 0;
}
static The principle and of modifying function static Decorating global variables has the same effect , But draw the ladle according to the gourd , There's no show here. Parents can try it .
summary
Global static variables
advantage : Both object methods and class methods can access and modify global static variables , And external classes cannot call static variables , After definition, it will only point to the fixed pointer address , For all objects , Save a space .
shortcoming : The life cycle of existence is long , From definition to the end of the program .
Local static variable
advantage : After definition, only one value will exist , Each call uses the value of the memory address of the same object , Not recreated , Save a space , Can only be used in this local code block .
shortcoming : The life cycle of existence is long , From definition to the end of the program , Can only be used in this local code block .
版权声明
本文为[Computer white]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210546128284.html
边栏推荐
- Unity animation creates sequence frame code and generates animationclip
- Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
- Addition, deletion, modification and query of advanced MySQL data (DML)
- Go limit depth traversal of files in directory
- Explore ASP Net core read request The correct way of body
- Elastic box model
- Syntax Error: TypeError: this. getOptions is not a function
- High paid programmer & interview question series 91 limit 20000 loading is very slow. How to solve it? How to locate slow SQL?
- Linux中,MySQL的常用命令
- 中创存储|想要一个好用的分布式存储云盘,到底该怎么选
猜你喜欢
Unity animation creates sequence frame code and generates animationclip
浅谈数据库设计之三大范式
LeetCode 542、01 矩阵
BMP JPEG picture to vector image contourtrace
The more you use the computer, the slower it will be? Recovery method of file accidental deletion
Common commands of MySQL in Linux
Addition, deletion, modification and query of MySQL advanced table
[stack and queue topics] - sliding window
MySQL basic collection
100天拿下11K,转岗测试的超全学习指南
随机推荐
JS arrow function user and processing method of converting arrow function into ordinary function
laravel 发送邮件
LeetCode 1346、检查整数及其两倍数是否存在
unity 功能扩展
[matlab 2016 use mex command to find editor visual studio 2019]
[stack and queue topics] - sliding window
MySQL basic collection
LeetCode 542、01 矩阵
深入探究ASP.NET Core读取Request.Body的正确方式
2021-09-02 unity project uses rider to build hot change project failure record of ilruntime
On the three paradigms of database design
go array
Selenium displays webdriverwait
UKFslam
JSX syntax rules
中创存储|想要一个好用的分布式存储云盘,到底该怎么选
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Go限制深度遍历目录下文件
Recommended usage scenarios and production tools for common 60 types of charts