📄 variable.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>变数</title>
<link rel="stylesheet" href="css/print.css" type="text/css">
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">C++ Gossip: 变数</a></h1>
<br>
<a href="LiteralConstant.html">字
面常数</a>
储存于记忆体之中,并与一个资料型态相关联,现在的问题是如果您要将一个数值储存在记忆体中,并在稍后再取回这个数值来使用,您该如何进行?显然的您无法
取得方才写下的字面常数,因为没有任何关于字面常数储存位置的资讯。<br>
<br>
变数(Variable)提供一个有名称的记忆体储存空间,一个变数关系至一个资料型态、一个变数本身的值与一个变数的位址值。<br>
<br>
变数资料型态决定了变数所分配到的记忆体大小;变数本身的值是指储存于记忆体中的某个数值,而您可以透过变数名称取得这个数值,这个数值又称为
rvalue或
read
value;而变数的位址值则是指变数所分配到的记忆体之位置,变数本身又称为lvalue或
location value。<br>
<br>
在C++中要使用变数,必须先宣告变数名称与资料型态,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
intNum; //
宣告一个整数变数 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
dblNum; // 宣告一个倍精度浮点数变数</span><br>
</div>
<br>
如上面所举的例子,您使用int、float、double、char、bool等关键字(Keyword)来宣告变数名称并指定其资料型态,变数在命名
时有一些规则,它不可以使用数字作为开头,也不可以使用一些特殊字元,像是*&^%之类的字元,而变数名称不可以与C++内定的关键字同
名,例如int、float、class等等。 <br>
<br>
变数的命名有几个风格,主要以清楚易懂为主,初学者为了方便,当使用一些简单的字母来作为变数名称,这会造成日后程式维护的困难,命名变数时发生同名的情
况也会增加。 <br>
<br>
在过去曾流行过匈牙利命名法,也就是在变数名称前加上变数的资料型态名称缩写,例如intNum用来表示这个变数是int整数资料型态,fltNum表示
一个float资料型态,然而随着现在程式的发展规模越来越大,这种命名方式已经不被鼓励。 <br>
<br>
过去的程式在撰写时,变数名称的长度会有所限制,但C++并不限制变数的长度,因而现在比较鼓励用清楚的名称或助忆文字来表明变数作用,通常会以小写字母
作为开始,并在每个单字开始时第一个字母使用大写,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
ageForStudent; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
ageForTeacher;</span><br>
</div>
<br>
像这样的名称可以让人一眼就看出这个变数的作用,还可以考虑用底线来区隔变数中的每个字面,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
age_for_student; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
age_for_teacher;</span><br>
</div>
<br>
在C++中宣告一个变数,就会配置一块记忆体空间给它,空间长度依宣告时的资料型态而定,被配置的这块空间中原先可能就有资料,也因此变数在宣告后的值是
不可预期的,所以应该在在变数宣告后初始其值,您可以使用“指定运算子”(Assignment operator)=来指定变数的值,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
ageForStudent = 0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
scoreForStudent = 0.0; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
levelForStudent = 'A';</span><br>
</div>
<br>
上例中三个变数分别被初始为0、0.0与'A',您还可以使用隐喻的方式来宣告变数并指定初值,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
ageForStudent(0);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
scoreForStudent(0.0);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
levelForStudent('A');</span><br>
</div>
<br>
<br>
这两种宣告方式在宣告变数的时候,同时指定变数的储存值,而您也看到我们如何指定字元给字元变数,字元在指定时需使用引数 ' '
来包括,在宣告变数之后,您可以直接呼叫变数名称来取得其所储存的值,下面这个程式是个简单的示范: <br>
<br>
<pre>#include <iostream> <br>using namespace std; <br><br>int main() { <br> int ageForStudent; <br> double scoreForStudent; <br> char levelForStudent; <br><br> cout << "\n年级\t得分\t等级"; <br> cout << "\n" << ageForStudent <br> << "\t" << scoreForStudent <br> << "\t" << levelForStudent <br> << "\n"; <br><br> ageForStudent = 5; <br> scoreForStudent = 80.0; <br> levelForStudent = 'B'; <br><br> cout << "\n年级\t得分\t等级"; <br> cout << "\n" << ageForStudent <br> << "\t" << scoreForStudent <br> << "\t" << levelForStudent <br> << "\n"; <br> <br> return 0;<br>}</pre>
<br>
在这个程式中,您首先宣告变数,但并没有初始其值,所以第一次显示时会出现不可预期的资料,而在指定变数的值之后,显示变数值时就会出现指定的资料了,执
行结果如下所示: <br>
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(0, 0, 0);"><small><span style="color: rgb(255, 255, 255);"></span><span style="color: rgb(255, 255, 255);">年级 得分 等级 </span><br style="color: rgb(255, 255, 255);">
<span style="color: rgb(255, 255, 255);">1073830176
4.8579e-270 @ </span><br style="color: rgb(255, 255, 255);">
<br style="color: rgb(255, 255, 255);">
<span style="color: rgb(255, 255, 255);">年级 得分
等级 </span><br style="color: rgb(255, 255, 255);">
<span style="color: rgb(255, 255, 255);">5
80 B</span></small></td>
</tr>
</tbody>
</table>
<br>
您也可以在使用宣告变数后,使用以下的建构子(Constructor)方式将变数的值初始为0:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">int
ageForStudent = int();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">double
scoreForStudent = double();</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">char
levelForStudent = char();</span><br>
</div>
<br>
有时候您一但将数值指定给变数之后,就不允许再将其它值指定给同一变数,则您可以在宣告变数时使用const关键字来限定,如果程式中有其它程式码试图改
变这个变数,编译器会先检查出这个错误,例如: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">const int maxNum = 10; </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">maxNum = 20;</span><br>
</div>
<br>
这一段程式码中的maxNum变数我们使用const来限定,所以它在指定为10之后,就不可以再指定值给它,所以第二次指定会被编译器指出错误,在g+
+编译器下,会出现这样的错误讯息: <br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace; color: rgb(255, 0, 0);">assignment of read-only
variable `maxNum'</span><br>
</div>
<br>
使用const来限定的变数,目的通常就是不希望其它的程式码来变动它的值,例如用于回圈计数次数的指定(回圈之后就会学到),或是像圆周率PI的指定。
<br>
<br>
如果要宣告无号的整数变数,则可以加上unsigned关键字,例如:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">unsigned int i ;</span><br>
</div>
<br>
bool型态的变数虽然也可视为整数型态,但不能在宣告变数时加上unsigned来修饰。<br>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -