当前位置:网站首页>011 C语言基础:C作用域规则
011 C语言基础:C作用域规则
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问。C 语言中有三个地方可以声明变量:
- 在函数或块内部的局部变量
- 在所有函数外部的全局变量
- 在形式参数的函数参数定义中
二:局部变量
在某个函数或块的内部声明的变量称为局部变量。只能被该函数或该代码内部的语句使用。局部变量在函数外部是不可知的。
假定所有的变量 a、b 和 c 是 main() 函数的局部变量。
实例:
#include <stdio.h>
int main(){
// 局部变量声明
int a, b;
int c;
// 实际初始化
a = 10;
b = 20;
c = a + b;
printf("value of a = %d, b = %d and c = %d \n", a, b, c);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim jububianliang.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc jububianliang.c -o jububianliang
┌──(rootkali)-[~/Desktop/c_test]
└─# ./jububianliang
value of a = 10, b = 20 and c = 30
三:全局变量
全局变量是定义在函数外部,通常是在程序的顶部。全局变量在整个程序生命周期内都是有效的,在任意的函数内部能访问全局变量。
全局变量可以被任何函数访问。也就是说,全局变量在声明后整个程序中都是可用的。
实例:
#include <stdio.h>
int c; // 全局变量声明
int main(){
int a, b; // 局部变量声明
a = 10;
b = 20;
c = a + b;
printf("value of a = %d, b = %d and c = %d \n", a, b, c);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim quanjubianliang.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc quanjubianliang.c -o quanjubianliang
┌──(rootkali)-[~/Desktop/c_test]
└─# ./quanjubianliang
value of a = 10, b = 20 and c = 30
在程序中,局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。
四:形式参数
函数的参数,形式参数,被当做该函数内的局部变量,他们会优先覆盖全局变量。
实例:
#include <stdio.h>
int a = 20; // 全局变量声明
int main(){
// 在主函数中声明局部变量
int a = 10;
int b = 20;
int c = 0;
int sum(int, int);
printf("value of a in main() = %d \n", a);
c = sum(a, b);
printf("value of a in main() = %d \n", c);
return 0;
}
// 添加两个整数的函数
int sum(int a, int b){
printf("value of a in sum() = %d \n", a);
printf("value of a in sum() = %d \n", b);
return a + b;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim xingshicanshu.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc xingshicanshu.c -o xingshicanshu
┌──(rootkali)-[~/Desktop/c_test]
└─# ./xingshicanshu
value of a in main() = 10
value of a in sum() = 10
value of a in sum() = 20
value of a in main() = 30
五:初始化局部变量和全局变量
当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。
定义全局变量时,系统会自动对其初始化。
如下所示:
| 数据类型 | 初始化默认值 |
|---|---|
| int | 0 |
| char | ‘’ |
| float | 0 |
| double | 0 |
| pointer | NULL |
边栏推荐
- Is the money invested in financial products guaranteed? Is there no more?
- Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
- 渗透测试-文件上传/下载/包含
- 缓存综合项目--秒杀架构
- Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
- PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
- Fplan powerplan instance
- A^2=E | 方程的解 | 这个方程究竟能告诉我们什么
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- [array]bm94 rainwater connection problem - difficult
猜你喜欢

静态时序分析-OCV和time derate

Système de collecte des journaux

Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?

Games101 job 7 improvement - implementation process of micro surface material

Why does C throw exceptions when accessing null fields?

基于MobileNet-Yolov4搭建轻量化目标检测

resnet152 辣椒病虫害图像识别1.0

Kotlin compose implicitly passes the parameter compositionlocalprovider
![[数组]BM94 接雨水问题-较难](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
[数组]BM94 接雨水问题-较难

Static timing analysis OCV and time derive
随机推荐
再探Handler(下)(Handler核心原理最全解析)
敏捷开发篇--Agile Development-自用
Mobilenet series (4): mobilenetv3 network details
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
Method of decoding iPhone certificate file
IOS development: understanding of dynamic library shared cache (dyld)
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
Common sense of Apple's unique map architecture
Six possible challenges when practicing Devops
fplan-电源规划
人间清醒:底层逻辑和顶层认知
从某种意义来讲,互联网业已成为了一个孵化器,一个母体
使用promise的基本功能【四、Promise源码】
Matlab | drawing of three ordinate diagram based on block diagram layout
Installing MySQL on Windows
I found a JSON visualization tool artifact. I love it!
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
Qchart note 2: add rollover display
Usage knowledge of mobile phones in new fields
List of best reading materials for machine learning in communication