当前位置:网站首页>分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
分享一道关于变量的选择题(内含全局变量、局部变量、变量的作用域、生命周期知识点)
2022-07-27 05:03:00 【vpurple__】
目录
推荐阅读顺序:
1.题目->3.答案->2.题目分析->4.题目知识点
1.题目
1、执行下面程序,正确的输出是( )
#include<stdio.h>
int x=5,y=7;
void swap()
{
int z;
z=x;
x=y;
y=z;
}
int main()
{
int x=3,y=8;
swap();
printf("%d,%d\n",x, y);
return 0;
}A: 5,7 B: 7,5 C: 3,8 D: 8,3
2.题目分析
这是一道很典型的考察全局变量、局部变量以及变量的作用域和生命周期的题目。
按数字编号观看更佳

3.题目答案
C:3,8
4.题目知识点
4.1局部变量与全局变量

在同一个范围内,局部变量命名不能相同,不能重复定义。

当局部变量和全局变量同名的时候,局部变量优先使用。
建议不要将全局变量和局部变量的名字起成一样的。

4.2变量的作用域、生命周期
作用域
作用域(scope)是程序设计概念,通常来说,一段程序代码中所用到的名字并不总是有效/可用的,而限定这个名字的可用性的代码范围就是这个名字的作用域。通俗的理解一下就是,这个变量在哪里能起到作用,哪里就是它的作用域。
1. 局部变量的作用域是变量所在的局部范围。
演示如下:

2. 全局变量的作用域是整个工程。

生命周期
变量的生命周期指的是变量的创建到变量的销毁之间的一个时间段 。
1. 局部变量的生命周期是:进入作用域生命周期开始,出作用域生命周期结束。
2. 全局变量的生命周期是:整个程序的生命周期。
边栏推荐
- torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()
- 蓝图-类视图方法
- numpy 数据类型转化
- Makefile is easy to understand and explain
- Three waiting methods of selenium and three processing methods of alert pop-up
- Pinball games
- 268.missing number of leetcode
- 后台实现spu管理
- LeetCode之6 ZigZag Conversion
- B1024 scientific counting method
猜你喜欢

Carmaker quick start lesson 4 developing 48V P1 hybrid system

How to quickly and effectively solve the problem of database connection failure

如何将Excel表格中的多列内容合并到一列

Flask请求数据获取与响应

The receiver sets the concurrency and current limit

Notes Series docker installation PostgreSQL 14

The concept of cloud native application and 15 characteristics of cloud native application

Day3 ---Flask 状态保持,异常处理与请求钩子

JDBC API details

李宏毅机器学习组队学习打卡活动day05---网络设计的技巧
随机推荐
C语言进制转换以及原补反码位运算介绍
redis发布订阅模式
Niuke sword refers to the path in the offer--jz12 matrix
DNSmasq使用总结
实用小工具: Kotlin 代码片段
cookie增删改查和异常
2021 Niuke multi school training camp 5 (question b)
用户的管理-限制
Idea remote debugging
后台实现spu管理
初识C语言——常量、变量
LeetCode之6 ZigZag Conversion
【codeforces round#800 B. Paranoid String】DP
C语言初阶——分支语句(if,switch)
Flask对数据库的查询以及关联
Dnsmasq Usage Summary
Student management system
Redis transaction
李宏毅机器学习组队学习打卡活动day05---网络设计的技巧
后台用户管理展示添加功能实现