当前位置:网站首页>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 |
边栏推荐
- nignx配置单ip限流
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- jmeter将上一个请求的结果作为下一个请求的参数
- Knowledge of iPhone certificate structure
- fplan-电源规划
- MySql最详细的下载教程
- Qchart note 2: add rollover display
- USB DRIVER
- Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
- 人间清醒:底层逻辑和顶层认知
猜你喜欢

PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL

Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录

2021:Greedy Gradient Ensemble for Robust Visual Question Answering

乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。

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

Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot

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

Implementation of window encryption shell

MySQL development environment

Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
随机推荐
解码苹果手机证书文件方法
Why does C throw exceptions when accessing null fields?
Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
Fplan power planning
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
Usage knowledge of mobile phones in new fields
Learn crypto from Buu (Zhou Geng)
Penetration test - directory traversal vulnerability
低代码开发平台NocoBase的安装
苹果手机证书构体知识
2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
mysql数据库基础:DQL数据查询语言
Building lightweight target detection based on mobilenet-yolov4
静态时序分析-OCV和time derate
jmeter将上一个请求的结果作为下一个请求的参数
2021:Greedy Gradient Ensemble for Robust Visual Question Answering
PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL
MATLAB | 三个趣的圆相关的数理性质可视化
GAMES101作业7提高-微表面材质的实现过程
从某种意义来讲,互联网业已成为了一个孵化器,一个母体