当前位置:网站首页>3、 C language storage class
3、 C language storage class
2022-07-28 14:53:00 【Rock magnon】
List of articles
1、auto Storage class
- effect :auto Storage class is the default storage class for all local variables
- Be careful : Only local variables can be modified
{ int a; auto int a;// The two are equivalent }
2、register Storage class
- effect : Used to define variables stored in registers . Both dynamic and static variables are stored in memory , When the program needs to be used , Then send the value to the calculator , But if you use a number frequently , In order to improve efficiency , You can put variables in CPU In the register of , It's called register variable
- Be careful : It cannot be used &, Because it has no memory location
- characteristic : Because it is stored in registers , Its access speed is particularly fast
register int a;
3、static Storage class
- effect :
- local variable : Throughout the life cycle of the program , Keep the existence of local variables , Instead of creating and destroying when entering and leaving the scope
- Global variables and functions : Can be called throughout the file
- Code implementation :
#include<stdio.h> void func1(void); static int count = 10;// Global variables ,static By default // int count = 10;// Equivalent to the above int main(){ while (count--) { func1(); } return 0; } void func1(void){ static int a = 4;// local variable , Exists throughout the program cycle , Will not be destroyed int b = 4;// local variable , Every time you enter the scope , Will be created , Destroyed when leaving b++; a++; printf("count=%d, a=%d, b=%d\n",count,a,b); } - Running results :
count=9, a=5, b=5 count=8, a=6, b=5 count=7, a=7, b=5 count=6, a=8, b=5 count=5, a=9, b=5 count=4, a=10, b=5 count=3, a=11, b=5 count=2, a=12, b=5 count=1, a=13, b=5 count=0, a=14, b=5
4、extern Storage class
- effect : References to global variables
- Code implementation :[ Reference link ]https://blog.csdn.net/qq_43280851/article/details/125424816
5、 Variable type and its scope
- Global variables : Global variables have global scope , Other files want to use this variable , Just use extern Just refer to this variable
- Static global variables : When the global variable is static After modification , It becomes a static global variable , Unlike global variables , Static local variables have file scope , That is, the static global variables of a file , It can only work on this file , This also makes it have the same name , But variables that are not in the same file can exist at the same time
- local variable : Local variables have only local scope , yes auto Storage class , When entering the scope, create , Destroy when exiting the scope
- Static local variables : Is initialized only once , Throughout the entire function cycle , But unlike global variables , It is only visible to functions that define themselves
5、 summary
- auto Is the default storage class for local variables ,static Is the default storage class for global variables
- When a variable needs to be accessed by all files , You can use global variables
- When a variable needs to be accessed by a single file field , You can use static global variables
- If global variables are accessed only by a single function , You can change this variable into a static local variable
- In the use of static variable , It has no reentry function , Initialize only once
边栏推荐
- BGP experiment
- Crawler: from entry to imprisonment (II) -- Web collector
- Pointers and arrays (7)
- Copy excel row to specified row
- SSH service
- The 35 required questions in MySQL interview are illustrated, which is too easy to understand
- 多商户商城系统功能拆解17讲-平台端订单列表
- SwiftUI 的动画机制
- 如何在 Core Data 中进行批量操作
- Swiftui layout - alignment
猜你喜欢

国产数据库的红利还能“吃”多久?

The method of implementing simple student achievement management system with C language

多商户商城系统功能拆解17讲-平台端订单列表

When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash

Redis-Redis在Jedis中的使用

Cv:: mat conversion to qimage error

linux安装mysql

@DS('slave') 多数据源兼容事务问题解决方案

Penguin side: why not recommend using select *?

十、时间戳
随机推荐
Excel VBA 免密查看VBE加密代码
Redis persistence
Digital transformation security issues occur frequently, and Shanshi Netcom helps build a digital government
用 Table 在 SwiftUI 下创建表格
【LeetCode】 贴纸拼词(动态规划)
The method of implementing simple student achievement management system with C language
实时切换 Core Data 的云同步状态
8、 Picker usage drop-down box selection effect
C# 读取ini文件、键值对操作
Many "double first-class" universities have launched the research guarantee and prediction name!
Tdengine helps Siemens' lightweight digital solutions
C# 获取当前路径7种方法
爆肝整理JVM十大模块知识点总结,不信你还不懂
Cv:: mat conversion to qimage error
我正在使用中的博客创作工具
Animation mechanism of swiftui
Various pitfalls encountered in UI development
Node file operation
UI开发中所遇到的各种坑
如何只降3D相机不降UI相机的分辨率