当前位置:网站首页>c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
2022-06-26 09:33:00 【坦桑尼亚奥杜威峡谷能人】
宏定义
// 宏定义只是替身,不需要小括号,一般命名用大写字母
#define B(c) c*c
main(){
int m=5, n=8,k;
k=10*B(m+n); // 是 10*5+8*5+8=98 而不是10*(5+8
)*(5+8)
printf("%d",k);
}
局部变量
定义变量可能有3种情况:
(1)在函数的开头定义;
(2)在函数内的复合语句内定义;
(3)在函数的外部定义。
具体定义:
在一个函数内部定义的变量只在本函数范围内有效,也就是说只有在本函数内才能引用它们,在此函数以外是不能使用这些变量的。在复合语句内定义的变量只在本复合语句范围内有效,只有在本复合语句内才能引用它们。在该复合语句以外是不能使用这些变量的,以上这些称为“局部变量”。
全局变量
定义:
在函数外部定义的变量属于外部变量也成全局变量或全程变量。
全局变量可以为本文件的其他函数所使用的。
它的有效范围是定义开始到本源文件结束。
全局变量与局部变量同名
局部变量的存储类别
1. 自动变量
用 auto 关键字声明。
也可以不写auto,默认隐含为自动存储类别。
函数中的形参和局部变量都属于此类型。在调用函数时,系统自动给变量分配存储空间,在调用结束时会自动释放此空间。
int f(int a){
auto int b,c=3;
}
2. 静态局部变量
用 static 关键字声明。
希望在函数中的局部变量在结束后不消失而继续保留原值,即占用的存储空间不变,在下一次调用时,可以继续使用其值,这个时候就需要使用局部静态变量来声明。
3.寄存器变量
用 register 关键字声明。
一般来说静态变量、动态变量都谁存储在内存中,每次用某个变量的值,都会先从内存中取出来,送到计算器,如果需要存数,在存到内存中。次数太多的话,10000次循环?这个过程就太耗费时间,为提高效率,可以吧这些变量存到cpu的寄存器中,需要时直接从寄存器取,因为寄存器的读取比内存读取快很多,所以这些变量就叫寄存器变量。
如:
register int a;
全局变量的存储类别:
全局变量都是存放在静态存储区的,它的生命周期是固定的,存在于整个程序运行中。只是其作用于需要看不同的存储类别。如以下几种情况
声明为外部变量 用 extern 关键字。
声明为进本文件可用 用 static 关键字
1. 在一个文件内部扩展外部变量的作用域
2. 将外部变量的作用域扩展到其他文件
3. 将外部变量的作用于限制在本文件内
边栏推荐
- 工企专利匹配数据(数十万数据量)1998-2014年
- QPM suspended window setting information
- The first techo day Tencent technology open day, 628
- online trajectory generation
- 0 basic how to make a cool leadership cockpit?
- Edge computing is the sinking and extension of cloud computing capabilities to the edge and user sides
- Common SQL add / delete / modify query statements
- Jz2440--- using uboot burning program
- MapReduce&Yarn理论
- Construction practice of bank intelligent analysis and decision-making platform
猜你喜欢
【CVPR 2019】Semantic Image Synthesis with Spatially-Adaptive Normalization(SPADE)
我的创作纪念日
Badge series 4: use of circle Ci
Badge series 8: generate a personalized Badge
Practice of production control | dilemma on assembly rack
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation
VI summary of common commands
"One week's work on Analog Electronics" - optocoupler and other components
Install new version cmake & swig & tinyspline
Optimization of power assisted performance of QPM suspended window
随机推荐
Merrill Lynch data helps State Grid Hubei "golden eye" accurately identify abnormal power consumption
There is a strong demand for enterprise level data integration services. How to find a breakthrough for optimization?
进入页面输入框自动获取焦点
Upgrade idea to 2021.2 shortcut keys
Throttling, anti chattering, new function, coriolism
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.npm ER
Common SQL add / delete / modify query statements
install realsense2: The following packages have unmet dependencies: libgtk-3-dev
Master data management of scientific research institutes? Suppliers or customers? I am a correspondent
力扣------从数组中移除最大值和最小值
【CVPR 2021】 Lifelong Person Re-Identification via Adaptive Knowledge Accumulation
Redis 新手入门
Jz2440 - - - utiliser le programme de gravure uboot
"One week's work on Analog Electronics" - power amplifier
Halcon photometric stereoscopic
Kubernetes cluster deployment (v1.23.5)
【CVPR 2021】Joint Generative and Contrastive Learning for Unsupervised Person Re-identification
Badge series 7: use of codacy
Flink入门——单词统计
The most complete and simple nanny tutorial: deep learning environment configuration anaconda+pychart+cuda+cudnn+tensorflow+pytorch