当前位置:网站首页>011 C language basics: C scope rules
011 C language basics: C scope rules
2022-06-27 04:24:00 【Prison plan progress 50%】
List of articles
One : summary
In any kind of programming , Scope is the area where variables defined in a program exist , Beyond that area variables cannot be accessed .C There are three places in the language where variables can be declared :
- Local variables inside a function or block
- Global variables outside all functions
- In the function parameter definition of formal parameters
Two : local variable
Variables declared inside a function or block are called local variables . Can only be used by the function or statements inside the code . Local variables are unknowable outside the function .
Assume all variables a、b and c yes main() A local variable of a function .
example :
#include <stdio.h>
int main(){
// Local variable declaration
int a, b;
int c;
// Actual initialization
a = 10;
b = 20;
c = a + b;
printf("value of a = %d, b = %d and c = %d \n", a, b, c);
return 0;
}
result :
┌──(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
3、 ... and : Global variables
Global variables are defined outside functions , Usually at the top of the program . Global variables are valid throughout the life of the program , You can access global variables inside any function .
Global variables can be accessed by any function . in other words , Global variables are available throughout the program after declaration .
example :
#include <stdio.h>
int c; // Global variable declaration
int main(){
int a, b; // Local variable declaration
a = 10;
b = 20;
c = a + b;
printf("value of a = %d, b = %d and c = %d \n", a, b, c);
return 0;
}
result :
┌──(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
In the program , The names of local and global variables can be the same , But in a function , The value of a local variable will override the value of a global variable .
Four : Formal parameters
The parameters of the function , Formal parameters , Is treated as a local variable in the function , They override global variables first .
example :
#include <stdio.h>
int a = 20; // Global variable declaration
int main(){
// Declaring local variables in the main function
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;
}
// Add a function of two integers
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;
}
result :
┌──(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
5、 ... and : Initializing local and global variables
When a local variable is defined , The system will not initialize it , You have to initialize it yourself .
When defining global variables , The system will automatically initialize it .
As shown below :
| data type | Initialize defaults |
|---|---|
| int | 0 |
| char | ‘’ |
| float | 0 |
| double | 0 |
| pointer | NULL |
边栏推荐
猜你喜欢

Matlab | visualization of mathematical properties related to three interesting circles

快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码

Kotlin compose implicitly passes the parameter compositionlocalprovider

如何系统学习LabVIEW?

Products change the world

MATLAB | 三个趣的圆相关的数理性质可视化

Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan

微服务系统设计——分布式定时服务设计

Mobilenet series (4): mobilenetv3 network details

MySQL development environment
随机推荐
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
fplan-布局
微服务系统设计——服务注册与发现和配置设计
MATLAB | 基于分块图布局的三纵坐标图绘制
低代码开发平台NocoBase的安装
021 C语言基础:递归,可变参数
Microservice system design - service fusing and degradation design
Fplan powerplan instance
微服务系统设计——服务链路跟踪设计
Interview-01
mysql数据库基础:DQL数据查询语言
【B站UP DR_CAN学习笔记】Kalman滤波2
nignx配置单ip限流
办公室VR黄片,骚操作!微软HoloLens之父辞职!
MATLAB | 三个趣的圆相关的数理性质可视化
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
通信中的机器学习最佳阅读资料列表
Common programming abbreviations for orbit attitude
017 C语言基础:位域和typedef
MySQL development environment