蓝桥杯零基础到获奖-第3章 C++ 变量和常量
文章目录
- 一、变量和常量
- 1.变量的创建
- 2.变量初始化
- 3.变量的分类
- 4.常量
- 4.1 字⾯常量
- 4.2 #define定义常量
- 4.3 const 定义常量
- 4.4 练习
- 练习1:买票https://www.nowcoder.com/practice/0ad8f1c0d7b84c6d8c560298f91d5e66
- 练习2:A + B 问题https://www.luogu.com.cn/problem/B2007
- 练习3:鸡兔同笼
一、变量和常量
1.变量的创建
了解清楚了类型,我们使⽤类型做什么呢?类型是⽤来创建变量的。
什么是变量呢?把经常变化的值称为变量,不变的值称为常量。
变量创建的语法形式是这样的:
data_type name;
| |
| |
数据类型 变量名
int age; //整型变量
char ch; //字符变量
double weight; //浮点型变量
变量的命名规则遵循以下原则:
• 变量名只能由字⺟、数字和下划线组成,且必须以字⺟或下划线开头。
• 变量名不能以纯数字开头,也不能包含特殊字符,如空格、连字符等。
• 变量名不能使⽤语⾔的关键字,如 int、char、if等。
• 变量名应具有意义,有助于理解变量的含义和⽤途。
• 变量名应简短明了,避免使⽤过⻓的名称。
• 变量名应区分⼤⼩写,例如 myVariable 和 myvariable 被视为两个不同的变量。
2.变量初始化
变量在创建的时候就给⼀个初始值,就叫初始化。
int age = 18;
char ch = 'w';
double weight = 48.0;
unsigned int height = 100;
3.变量的分类
• 全局变量:在⼤括号外部定义的变量就是全局变量全局变量的使⽤范围更⼴,整个⼯程中想使⽤,都是有办法使⽤的
• 局部变量:在⼤括号内部定义的变量就是局部变量局部变量的使⽤范围是⽐较局限,只能在⾃⼰所在的局部范围内使⽤的
#include <iostream>
using namespace std;
int global = 2023; //全局变量
int main()
{int local = 2018; //局部变量cout << local << endl;cout << global << endl;return 0;
}
如果局部和全局变量,名字相同呢?
#include <iostream>
using namespace std;
int n = 1000;
int main()
{int n = 10;cout << n << endl; //打印的结果是多少呢?return 0;
}
其实当局部变量和全局变量同名的时候,局部变量优先使⽤。
• 未初始化状态下的全局变量和局部变量
//未初始化的局部变量
#include <iostream>
using namespace std;
int main()
{//局部变量int a;char c;float f;double d;cout << "int:" << a << endl;cout << "char:" << c << endl;cout << "float:" << f << endl;cout << "double:" << d << endl;return 0;
}
//未初始化的全局变量
#include <iostream>
using namespace std;
//全局变量
int a;
char c;
float f;
double d;
int main()
{cout << "int:" << a << endl;cout << "char:" << c << endl;cout << "float:" << f << endl;cout << "double:" << d << endl;return 0;
}
在Dev-C++下:
局部变量: 全局变量:
4.常量
常量就是不能被改变的值,通常我们会使⽤三种常量:
• 字⾯常量
• #define 定义的常量
• const 定义的常量
下⾯分别介绍⼀下。
4.1 字⾯常量
整型常量: 100,-5,0,0x123 整型常量⼀般可以写成10进制、8进制、16进制。
10进制数字,如: 6、17、22、123
8进制数字,⼀般是数字0开头的,⽐如: 012 , 016
16进制数字,⼀般是0x开头的数字,⽐如: 0x123 , 0xFF
字符常量: ‘a’
浮点型常量: 3.14 , 1E6 (科学计数法的形式)
4.2 #define定义常量
有时候也会使⽤ #define 来定义常量,⽐如:
代码举例:
#include <iostream>
using namespace std;
#define M 100
#define CH 'x'
#define PI 3.14159
int main()
{cout << M << endl;cout << CH << endl;cout << PI << endl;return 0;
}
这⾥定义的 M、CH、PAI 都是常量,可以直接使⽤,但是不能被修改。
使⽤ #define 定义常量的时候是不关注类型的,只关注常量的名字叫啥,常量的值是啥,编译在处
理这种常量的时候就是直接替换,在出现常量名字的地⽅,通通替换成常量的内容。
上⾯的代码被编译器替换后就是:
#include <iostream>
using namespace std;
int main()
{cout << 100 << endl;cout << 'x' << endl;cout << 3.14159 << endl;return 0;
}
4.3 const 定义常量
除了上⾯的⽅式之外,C++中还可以使⽤ const 来定义常量,这种常量会有具体的类型。⽐#define 定义常量更加严谨。语法形式如下:
⽐如:const double PI = 3.14159;
使⽤⼀下:
#include <iostream>
using namespace std;
const double PI = 3.14159;
int main()
{int r = 0;cin >> r;cout << "周⻓:" << 2 * PI * r << endl;cout << "⾯经:" << PI * r * r << endl; //PI = 3.14;//这种写法是错误的,常量不能被修改return 0;
}
习惯上,这种常量的名字⼀般会写成⼤写,⽽普通变量的名字不会全⼤写,这样就可以做⼀个区分。
使⽤ const 定义的常量的好处:
• 增加了程序的可读性, PI ⽐ 3.14159 更加容易理解和书写、使⽤。
• 增加了程序的可维护性,如果改变常量的值,只要在定义的部分修改,使⽤的地⽅也就随之改变
了,做到了"⼀改全改"的效果。
• 常量是不能修改的,当然 const 定义的常量⾃然也不能修改
#include <iostream>
using namespace std;
int main()
{const int num = 10;num = 20; //修改num,编译器会报错的return 0;
}
4.4 练习
练习1:买票https://www.nowcoder.com/practice/0ad8f1c0d7b84c6d8c560298f91d5e66
#include<bits/stdc++.h>
using namespace std;
int main()
{int x;cin>>x;cout<<x*100;return 0;
}
练习2:A + B 问题https://www.luogu.com.cn/problem/B2007
#include<bits/stdc++.h>
using namespace std;
int main()
{int x,y;cin>>x>>y;cout<<x+y;}
练习3:鸡兔同笼
https://www.luogu.com.cn/problem/B2614
#include<bits/stdc++.h>
using namespace std;
int main()
{cout<<12<< " "<<23;return 0;
}