📄 c82.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>枚举类型 </title>
<script language="javascript">
var prePage="c/c8/c81.htm";
var nextPage="c/c8/c83.htm";
function showwin(url,winname,properties){
window.open(url,winname,properties)
}
</script>
<link rel="stylesheet" href="../cstyle.css" type="text/css">
<bgsound src="../voice/c82.au" loop="1">
</head>
<body background="../img/mainback.jpg" bgproperties="fixed">
<h2 align="center"><font face="楷体_GB2312"><a name="_top"></a>8.2 枚举类型</font></h2>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="center"><a href="c82.htm#c821.html#c821">枚举类型</a></td>
<td width="50%" align="center"><a href="c82.htm#c822.html#c822">练习题</a></td>
</tr>
</table>
<hr>
<h3><a name="c821"></a>1.枚举类型</h3>
<blockquote>
<p>到目前为止, 我们已经学会了基本类型。 例如, 整型,
以及诸如结构之类的构造类型。 我们还讨论过联合类型。 现在,
我们就要来学习一种新的类型: <font color="#FF0000">枚举</font>。<br>
<br>
C 语言程序中用作标志的变量的性质之一是,
它通常被赋值为两个值之一: 或者是真, 或者是假。<br>
<br>
为了防止疏忽地把真或假之外的值赋给了作为标志使用的变量,
我们该怎么办呢? 再说, 这样的变量通常说明成 int 或 short int 类型,
它们在程序中的作用也不会是明确的。<br>
<br>
如果我们定义了一个标志类型的变量, 那么,
是否有什么方法能让我们指定,
说明成这种类型的变量只能被赋值为真或假吗? <br>
<br>
这种能力正好由枚举数据类型提供了。<br>
枚举数据类型的定义必须以关键字 <font color="#FF0000">enum</font>
开始。紧跟在这个关键字后面的是枚举数据类型的名字。然后是可以赋值给此类型的值表。<br>
<br>
怎样定义一个枚举数据类型呢? 我们来看一个例子吧! <br>
<br>
C 语言的语句 <font color="#000080">enum flag { 真 , 假 };</font>
定义了一个数据类型 flag。<br>
<br>
在程序中, 这个数据类型可以被赋值为真或假, 而不能是其它的值。<br>
<br>
为了说明一个 enum flag 类型的变量, 我们必须再次使用关键字 enum,其后是枚举类型名,
再后面是变量表。<br>
因此, 语句 enum flag end_of_data, match_found; 定义了两个 flag
类型的变量──end_of_data 和 match_found。</p>
<p class="note">注意: 能够赋值给这些变量的可能的值是真和假,
或者其它说明成同一类型的另一个枚举变量的值。</p>
<p>因此, 下面的语句 end_of_data = 真 ; 是有效的。语句 if (match_found ==
假 ) ... 也是有效的。<br>
<br>
C 编译器是怎样处理枚举值的呢? <br>
<br>
C
编译器实际上把枚举值看成常量来对待。编译器从表中第一个名字开始,
把从 0 开始的连续整数依次赋值给这些名字。<br>
<br>
假定我们定义了一个枚举类型: <br>
enum direction { up, down, left, right };<br>
已赋值给这些名字的整数值如下表所示: </p>
<table border="5" width="85%" bgcolor="#CCFFFF" bordercolor="#FF9933" cellspacing="0"
cellpadding="2">
<tr>
<td width="25%" align="center">up</td>
<td width="25%" align="center">down</td>
<td width="25%" align="center">left</td>
<td width="25%" align="center">right</td>
</tr>
<tr>
<td width="25%" align="center">0</td>
<td width="25%" align="center">1</td>
<td width="25%" align="center">2</td>
<td width="25%" align="center">3</td>
</tr>
</table>
<p>假如我们执行了如下那样的语句:<br>
enum direction this_direct;<br>
this_direct = down;<br>
那么, 赋值给 this_direct 的是值 1。(而不是名字 "down")<br>
<br>
如果希望某个枚举值具有一个指定的整数值,
那么可以在定义数据类型时, 把这个整数值赋值给那个枚举值。<br>
表中随后出现的枚举值将被顺序地赋值为从该整数值+1开始的连续整数值。<br>
<br>
假定我们定义了一个类型 direction: <br>
enum direction { up,down, left = 10, right};<br>
编译器把值 0 赋给 up, 1 赋给 down, 而把 10 赋给 left,
因为它被明显地赋以这个值。 此后, 由于 right 在表中是紧跟在 left
之后的, 故它被赋以 11。</p>
<table border="5" width="85%" bgcolor="#CCFFFF" bordercolor="#FF9933" cellspacing="0"
cellpadding="0">
<tr>
<td width="25%" align="center">up</td>
<td width="25%" align="center">down</td>
<td width="25%" align="center">left</td>
<td width="25%" align="center">right</td>
</tr>
<tr>
<td width="25%" align="center">0</td>
<td width="25%" align="center">1</td>
<td width="25%" align="center">10</td>
<td width="25%" align="center">11</td>
</tr>
</table>
<p>在用枚举类型变量写程序时,
不要依赖枚举值是作为整型常量处理的这一事实。取而代之的,
应该把这些变量当作特定的变量类型来对待。<br>
<br>
在定义枚举类型时, 允许的变化形式与定义结构时允许的那些类似:
在定义类型时, 可以省略数据类型名字,
并且说明某种特定枚举数据类型的变量。作为一个例子, 语句 <br>
enum { east, west, south, north } location;<br>
定义了一个 (<font color="#FF0000">没有名字的</font>) 枚举数据类型,
并且说明了一个该类型的变量 location。<br>
<br>
我们还应该注意:如果考虑到作用域, 那么,
枚举类型定义的作用域是与结构以及变量定义的作用域类似的。顺便说一下,
在定义枚举数据类型时,
必须确保该枚举值的名字在同一作用域内定义的其它变量和枚举值的名字之间是唯一的。</p>
<p align="right"><a href="c82.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<hr>
<h3><a name="c822"></a>2.练习题</h3>
<blockquote>
<p><a
href="javascript:showwin('c82_21.htm',null,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=600,height=600')">练习题一</a><img
src="../img/lefthand.gif" alt="lefthand.jpg (983 bytes)" WIDTH="45" HEIGHT="20"></p>
<p align="right"><a href="c82.htm#_top.html#_top">返回页首</a></p>
</blockquote>
<p align="center"><a href="http://www.nec.sjtu.edu.cn/support/Course/C/c/c8/c83.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 + -