当前位置:网站首页>56.【全局变量和局部变量专题】
56.【全局变量和局部变量专题】
2022-08-02 20:05:00 【李在奋斗……】

【单独定义一个全局变量】
#include<iostream>
using namespace std;
int a = 0;
void fun1()
{
a += 5;
}
void fun2()
{
a += 10;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}
==========================
【定义一个全局,一个局部(同名)】
#include<iostream>
using namespace std;
int a=0;
void fun1()
{
a += 5;
}
void fun2()
{
int a=0; //定义局部同名变量
a += 10;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}
==========================
①【定义一个局部不同名,一个局部同名。不同函数体】
#include<iostream>
using namespace std;
int a = 0;
void fun1()
{
int b = 3; //定义不同名局部变量
a += 5+b;
}
void fun2()
{
int a = 0; //定义同名局部变量.
a += 10;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}

==========================
②【定义一个局部不同名,一个局部同名。同一个函数体】
#include<iostream>
using namespace std;
int a = 0;
void fun1()
{
a += 5;
}
void fun2()
{
int a = 0; //定义同名局部变量.
int b = 3; //定义不同名局部变量
a += 10+b;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}

边栏推荐
- 有效解决MySQL报错:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES)
- 新增指令 v-memo
- pytorch的tensor创建和操作记录
- 广东省数字经济发展指引 1.0之建成数据安全保障体系
- arm64麒麟安装paddlehub(国产化)
- Parse common methods in the Collection interface that are overridden by subclasses
- OpenCV开发中的内存管理问题
- postgresql autovaccum自动清理
- SQL Server数据类型转换函数cast()和convert()详解
- Linphone 被叫方如何解析来电SIP消息中的自定义头消息
猜你喜欢

接口测试常用工具及测试方法(入门篇)

特拉维夫大学 | Efficient Long-Text Understanding with Short-Text Models(使用短文本模型进行高效的长文本理解)

SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)

Introduction of uncommon interfaces of openlayers

In action: 10 ways to implement delayed tasks, with code!

广东省数字经济发展指引 1.0之建成数据安全保障体系

Helm基础知识

Day35 LeetCode

线程安全(上)

Parse the commonly used methods in the List interface that are overridden by subclasses
随机推荐
新增指令 v-memo
pytorch的tensor创建和操作记录
Geoserver+mysql+openlayers2
【数据分析】:什么是数据分析?
Meta 与苹果的元宇宙碰撞
OP-5,输入/输出信号范围-一信号处理能力
「每周译Go」这次我们来点不一样的!--《How to Code in Go》系列上线
[21 Days Learning Challenge] Bubble Sort and Insertion Sort
SQL Server安装教程
网上那么多教人赚钱的方法,但是你实际上是靠什么赚钱的呢?
信息学奥赛一本通(1257:Knight Moves)
李沐动手学深度学习V2-bert预训练数据集和代码实现
【LeetCode】1374. 生成每种字符都是奇数个的字符串
LeetCode 622 设计循环队列[数组 队列] HERODING的LeetCode之路
The time series database has been developed for 5 years. What problem does it need to solve?
LeetCode - 105. 从前序与中序遍历序列构造二叉树;023.合并K个升序链表
「 每日一练,快乐水题 」1374. 生成每种字符都是奇数个的字符串
磁盘分区的知识
软件测试的流程规范有哪些?具体要怎么做?
【LeetCode】1161. 最大层内元素和