完整目录、平台简介、安装环境及版本:参考《21天学C++ 概览》
三、变量和常量
3.1 什么是变量
3.1.1 数据存于内存
- 内存看作是文件柜,文件柜由许多小格子组成,每一格都按顺序编号,这些编号称之为内存地址。
- 如变量myVariable从内容地址103开始,可能占用1个或多个内存地址。
3.1.2 变量大小
编译器需要根据变量类型预留相应空间,是1个字节、2个字节、4个字节还是更多。
#include <iostream>
int main()
{
using std::cout;
cout << "The size of an int is:\t\t" << sizeof(int) << " bytes.\n";
cout << "The size of a short int is:\t" << sizeof(short) << " bytes.\n";
cout << "The size of a long int is:\t" << sizeof(long) << " bytes.\n";
cout << "The size of a char is:\t\t" << sizeof(char) << " bytes.\n";
cout << "The size of a float is:\t\t" << sizeof(float) << " bytes.\n";
cout << "The size of a double is:\t" << sizeof(double) << " bytes.\n";
cout << "The size of a bool is:\t" << sizeof(bool) << " bytes.\n";
return 0;
}
执行结果如下:
The size of an int is: 4 bytes.
The size of a short int is: 2 bytes.
The size of a long int is: 4 bytes.
The size of a char is: 1 bytes.
The size of a float is: 4 bytes.
The size of a double is: 8 bytes.
The size of a bool is: 1 bytes.
请按任意键继续. . .
3.1.3 有符号和无符号
- 整数分为有符号和无符号,有符号可正可负,无符号只能为正;
- 有符号和无符号所占字节数相同;
3.2 操作变量
3.2.1 定义变量
- 变量类型+一个或多个空格+变量名称+分号,如int myVariable;
- 声明变量时,计算机就为其分配内存,此内存中的值就是变量的值;
- 变量大小写敏感;
命名方式:
- 下划线表示法:my_variable
- 驼峰命名法(myVariable):C++常用,通过类封装变量,变量返回值就是变量类型。
- 匈牙利命名法(iMyVariable):C语言常用,可以直接反映变量类型。C语言中如果根据变量名不知道变量类型,则需要查找变量声明的地方,变量声明可能在文件的任何地方。
3.2.2 一次创建多个变量
int myAge, myWeight;
不能在一条语句中定义不同类型的变量。
3.2.3 变量初始化、赋值
int myWidth=5;
int myWidth=5,myOther,myHeight=4;
int myArea=myWidth*myHeight;
初始化和赋值的区别在于初始化必须在创建变量的同时进行。
3.2.4 typedef
typedef unsigned short int USHORT;
程序多处使用unsigned short int 即繁琐又容易出错,typedef可以创建别名/同义词;
别名和创建新变量有本质区别。
#include <iostream>
typedef unsigned short int USHORT;
int main(){
USHORT Width = 5;
USHORT Length = 10;
USHORT Area = Width * Length;
cout << "Width:" << Width << "\n";
cout << "Length: " << Length << endl;
cout << "Area: " << Area <<endl;
return 0;
}
执行结果如下:
Width:5
Length: 10
Area: 50
请按任意键继续. . .
3.2.5 无符号和有符号的回绕
无符号整型达到其最大值时,该数值将回绕到0;
#include <iostream>
int main(){
unsigned int maxVal=0xFFFFFFFF;
std::cout<<maxVal+1<<std::endl;
unsigned int minVal=0;
std::cout<<minVal-1<<std::endl;
int maxVar=0x7FFFFFFF;
std::cout<<maxVar+1<<std::endl;
int minVar=0x80000000;
std::cout<<minVar-1<<std::endl;
return 0;
}
执行结果如下:
0
4294967295
-2147483648
2147483647
请按任意键继续. . .
3.2.6 字符型变量
只占一个字节;
#include <iostream>
int main(){
for (int i = 32; i<128; i++)
std::cout << (char) i;
return 0;
}
执行结果如下:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~请按任意键继续. . .
3.2.7 转义字符
转移字符改变\后面字母的意义;
3.2.8 定义变量注意事项
- 建议1:定义变量时先给出其类型,而后再给出其名称;
- 建议2:请使用易认易懂的变量名;
- 建议3:请记住C++区分大小写;
- 建议4:请务必记住各种变量在内存中所占的字节数以及每种变量可存储什么样的值;
- 不建议1:不要使用C++关键字作为变量名;
- 不建议2:不要给无符号变量赋负值;
3.2.9 关键字
3.3 常量
创建一个常量的同时,必须对其初始化,并且此后不能再向其赋值
字面常量,如39
- 符号常量,两种定义方法:
- #define studentsPerClass 15 //传统旧方法,已废弃
- #const unsigned int studentPerClass=15; //规定常量类型
枚举型常量(无符号整型)
Enum Color {RED=100, BLUE, GREEN=500, WHITE, BLACK=700};
将Color定义为一个新类型,即枚举类型;
将RED初始化为100(默认为0),依次加1递增,如BLUR=101,WHITE=501
#include <iostream>
int main()
{
enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
Days today;
today = Monday;
if (today == Sunday || today == Saturday)
std::cout << "\nGotta' love the weekends!\n";
else
std::cout << "\nBack to work.\n";
return 0;
}
执行结果如下:
Back to work.
请按任意键继续. . .
#include <iostream>
int main(){
const int Sunday = 0;
const int Monday = 1;
const int Tuesday = 2;
const int Wednesday = 3;
const int Thursday = 4;
const int Friday = 5;
const int Saturday = 6;
int today = Monday;
if (today == Sunday || today == Saturday)
std::cout << "\nGotta' love the weekends!\n";
else
std::cout << "\nBack to work.\n";
return 0;
}
执行结果如下:
Back to work.
请按任意键继续. . .