📄 c11.htm
字号:
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 3.0">
<title>变量</title>
<script language="javascript">
var prePage="c/c0/c02.htm";
var nextPage="c/c1/c12.htm";
function showwin(url, winname, properties){
window.open(url,winname,properties)
}
</script>
<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c11.au" loop="1">
</head>
<body background="../img/mainback.jpg" bgproperties="fixed">
<h2 align="center"><a name="_top"></a><font face="楷体_GB2312">1.1 变量</font></h2>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="center"><a href="c11.htm#c111.html#c111">基本概念</a></td>
<td width="50%" align="center"><a href="c11.htm#c112.html#c112">练习题</a></td>
</tr>
</table>
<hr>
<h3><a name="c111"></a>1.基本概念</h3>
<blockquote>
<table border="0" width="86%">
<tr>
<td width="28%" rowspan="5">C 语言中的变量可以被看成存储器地址,
我们可以把数字或字符写入其中,
或从中读出数字或字符。你可以把一个数,比如 1 赋给一个变量:</td>
<td width="17%" rowspan="5"><p align="right">数字或字符</td>
<td width="16%" rowspan="5"><img src="../img/flow.gif" width="84" height="15"
alt="flow.gif (2462 bytes)"></td>
<td width="14%">存储单元</td>
<td width="28%">地址</td>
</tr>
<tr>
<td width="14%"></td>
<td width="28%">000001</td>
</tr>
<tr>
<td width="14%">1</td>
<td width="28%">000002 (变量)</td>
</tr>
<tr>
<td width="14%"></td>
<td width="28%">000003</td>
</tr>
<tr>
<td width="14%"></td>
<td width="28%">000004</td>
</tr>
</table>
</blockquote>
<blockquote>
<p>当然,你也可以从000002 的地址中读出数据。</p>
<p> 在一个程序中可以使用很多变量。
这些变量必须逐一地分别给以名字。这些名字被称作为标识符。C
中的变量名只能够由英文字母,
下划线和数字组成。第一个字母必须是英文字母或下划线。看看下面正确和不正确的变量名:</p>
<blockquote>
<div align="center"><center><table border="0" width="500">
<tr>
<td width="14%" align="center">正确:</td>
<td width="19%" align="center">wiggly</td>
<td width="21%" align="center">cat_1</td>
<td width="24%" align="center">hot_dog</td>
<td width="22%" align="center">_hello</td>
</tr>
<tr>
<td width="14%" align="center">不正确:</td>
<td width="19%" align="center">$Z^**</td>
<td width="21%" align="center">1cat</td>
<td width="24%" align="center">Hot-Tub</td>
<td width="22%" align="center">rate%</td>
</tr>
</table>
</center></div>
</blockquote>
<p> 在 C
中大写字母和小写字母是不同的。在有些系统中, 仅头 8
个字符是有效的。因此, shakespesre 和 shakespencil
被认为是相同的。最后, 我们建议你给变量取有意义的名字。 这样,
可以使读者容易理解程序的内容。如:</p>
<blockquote>
<div align="center"><center><table border="0" width="60%">
<tr>
<td width="25%" align="center"><img src="../img/spade.jpg" alt="spear.jpg (6943 bytes)"
WIDTH="80" HEIGHT="20"></td>
<td width="25%" align="center"><img src="../img/heart.jpg" alt="heart.jpg (6895 bytes)"
WIDTH="80" HEIGHT="20"></td>
<td width="25%" align="center"><img src="../img/diamond.jpg"
alt="diamond.jpg (6695 bytes)" WIDTH="80" HEIGHT="20"></td>
<td width="25%" align="center"><img src="../img/club.jpg" alt="club.jpg (6985 bytes)"
WIDTH="80" HEIGHT="20"></td>
</tr>
<tr>
<td width="25%" align="center"><font color="#FF00FF">Spade</font></td>
<td width="25%" align="center"><font color="#FF00FF">Heart</font></td>
<td width="25%" align="center"><font color="#FF00FF">Diamond</font></td>
<td width="25%" align="center"><font color="#FF00FF">Club</font></td>
</tr>
</table>
</center></div>
</blockquote>
</blockquote>
<blockquote>
<p align="right"><a href="c11.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c112"></a>2.练习题</h3>
<blockquote>
<p><a
href="javascript:showwin('c11_21.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=600,height=500')">练习题一</a><img
src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
<p><a
href="javascript:showwin('c11_22.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=650,height=500')">练习题二</a><img
src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
<p align="right"><a href="c11.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c1/c12.htm"><img src="../img/next.gif" width="145" height="30"
alt="next.gif (3633 bytes)" border="0"></a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -