当前位置:网站首页>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 |
边栏推荐
- 微服务系统设计——分布式锁服务设计
- How can e-commerce products be promoted and advertised on Zhihu?
- 【B站UP DR_CAN学习笔记】Kalman滤波3
- math_ Number set (number set symbol) and set theory
- 面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?
- Network structure and model principle of convolutional neural network (CNN)
- Description of replacement with STM32 or gd32
- QChart笔记2: 添加鼠标悬停显示
- 微服务系统设计——微服务监控与系统资源监控设计
- 017 C语言基础:位域和typedef
猜你喜欢

2022-06-26: what does the following golang code output? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { type

Argo Workflows —— Kubernetes的工作流引擎入门
![[array]bm94 rainwater connection problem - difficult](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
[array]bm94 rainwater connection problem - difficult

fplan-电源规划

跟着BUU学习Crypto(周更)

How to systematically learn LabVIEW?

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

MySQL development environment

Kotlin compose compositionlocalof and staticcompositionlocalof

Fplan power planning
随机推荐
021 C语言基础:递归,可变参数
笔记本电脑没有WiFi选项 解决办法
通信中的机器学习最佳阅读资料列表
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
第2章 关键技术介绍
实践 DevOps 时,可能面临的六大挑战
007 C语言基础:C运算符
Building lightweight target detection based on mobilenet-yolov4
Golang Hello installation environment exception [resolved]
Microservice system design -- distributed cache service design
为什么 C# 访问 null 字段会抛异常?
[station B up dr_can learning notes] Kalman filter 2
Kotlin Compose 隐式传参 CompositionLocalProvider
Nignx configuring single IP current limiting
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
golang hello 安装环境异常【已解决】
Common sense of Apple's unique map architecture
Microservice system design - service fusing and degradation design
006 C语言基础:C存储类
如何让 EF Core 6 支持 DateOnly 类型