当前位置:网站首页>2. 变量及作用域
2. 变量及作用域
2022-07-29 03:33:00 【Timindream】
1.变量
即用即定义,且可用表达式初始化。
#include <iostream>
using namespace std;
int main()
{
double a = 12 * 3.25;
double b = a + 1.112;
cout << a << endl;
cout << b << endl;
a = a * 2 + b;
double c = a + b * a;
cout << c << endl;
return 0;
}
程序块{}内部作用域可定义域外同名变量,在该快里就隐藏了就外部变量。
#include <iostream>
using namespace std;
int main()
{
double a;
cin >> a;
{
int a = 1;
a = a * 10 + 4;
cout << a << endl;
}
cout << a << endl;
return 0;
}
for 循环可以定义局部变量。
#include <iostream>
using namespace std;
int main()
{
int i = 0;
for (int i = 0; i < 4; i++)
{
cout << i << endl; // 输出循环局部变量
}
cout << i << endl; // 输出main中的i
for (i = 0; i < 4; i++)
{
for (int i = 0; i < 4; i++) //每轮都重新定义局部变量i
{
cout << i << " ";
}
cout << endl;
}
return 0;
}
访问和内部作用域的变量同名的全局变量,要用全局作用域限定::。
#include <iostream>
using namespace std;
double a = 123;
int main()
{
double a = 256;
cout << a << endl; //局部变量a
cout << ::a << endl; //全局变量a
return 0;
}
边栏推荐
- xxxxx
- Introduction and advanced MySQL (13)
- Machine learning [numpy]
- 3.1 common neural network layer (I) image correlation layer
- Asynchronous callback future mode of concurrent mode
- web-uploader不能多文件上传
- Realize multi-level linkage through recursion
- HDU多校第二场 1011 DOS Card
- Web uploader cannot upload multiple files
- Simple understanding of Poe and UPS Technology
猜你喜欢
![LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路

Rongyun real-time community solution

NXP i.mx8mp-deepviewrt

Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP

July 28, 2022 Gu Yujia's study notes

Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules

Singleton mode (hungry and lazy)

Practical application cases of digital Twins - smart energy

Ten thousand words detailed Google play online application standard package format AAB

makefile详解
随机推荐
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
Bingbing learning notes: operator overloading -- implementation of date class
容斥原理
Introduction and advanced level of MySQL (12)
Military product development process - transition phase
深入C语言(3)—— C的输入输出流
暴力递归到动态规划 01 (机器人移动)
安装抓包证书
Simple understanding of Poe and UPS Technology
实例搭建Flask服务(简易版)
Notes on letter symbol marking of papers
04 | background login: login method based on account and password (Part 1)
Excel拼接数据库语句
Singleton mode (hungry and lazy)
Division and description of military technical documents
Redis之sentinel哨兵集群怎么部署
机器学习【Numpy】
1.5 nn. Module neural network (III)
for_each用法示例
Environment configuration stepping pit during colab use