source: public/doc/gnu-c/Enumeration-Types.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.0 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Enumeration Types (GNU C Language Manual)</title>
23
24<meta name="description" content="Enumeration Types (GNU C Language Manual)">
25<meta name="keywords" content="Enumeration Types (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="index.html" rel="up" title="Top">
33<link href="Defining-Typedef-Names.html" rel="next" title="Defining Typedef Names">
34<link href="Arrays-of-Variable-Length.html" rel="prev" title="Arrays of Variable Length">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Enumeration-Types"></span><div class="header">
59<p>
60Next: <a href="Defining-Typedef-Names.html" accesskey="n" rel="next">Defining Typedef Names</a>, Previous: <a href="Arrays.html" accesskey="p" rel="prev">Arrays</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Enumeration-Types-1"></span><h2 class="chapter">17 Enumeration Types</h2>
64<span id="index-enumeration-types"></span>
65<span id="index-types_002c-enumeration"></span>
66<span id="index-enumerator"></span>
67
68<p>An <em>enumeration type</em> represents a limited set of integer values,
69each with a name. It is effectively equivalent to a primitive integer
70type.
71</p>
72<p>Suppose we have a list of possible emotional states to store in an
73integer variable. We can give names to these alternative values with
74an enumeration:
75</p>
76<div class="example">
77<pre class="example">enum emotion_state { neutral, happy, sad, worried,
78 calm, nervous };
79</pre></div>
80
81<p>(Never mind that this is a simplistic way to classify emotional states;
82it&rsquo;s just a code example.)
83</p>
84<p>The names inside the enumeration are called <em>enumerators</em>. The
85enumeration type defines them as constants, and their values are
86consecutive integers; <code>neutral</code> is 0, <code>happy</code> is 1,
87<code>sad</code> is 2, and so on. Alternatively, you can specify values for
88the enumerators explicitly like this:
89</p>
90<div class="example">
91<pre class="example">enum emotion_state { neutral = 2, happy = 5,
92 sad = 20, worried = 10,
93 calm = -5, nervous = -300 };
94</pre></div>
95
96<p>Each enumerator which does not specify a value gets value zero
97(if it is at the beginning) or the next consecutive integer.
98</p>
99<div class="example">
100<pre class="example">/* <span class="roman"><code>neutral</code> is 0 by default,</span>
101 <span class="roman">and <code>worried</code> is 21 by default.</span> */
102enum emotion_state { neutral,
103 happy = 5, sad = 20, worried,
104 calm = -5, nervous = -300 };
105</pre></div>
106
107<p>If an enumerator is obsolete, you can specify that using it should
108cause a warning, by including an attribute in the enumerator&rsquo;s
109declaration. Here is how <code>happy</code> would look with this
110attribute:
111</p>
112<div class="example">
113<pre class="example">happy __attribute__
114 ((deprecated
115 (&quot;impossible under plutocratic rule&quot;)))
116 = 5,
117</pre></div>
118
119<p>See <a href="Attributes.html">Attributes</a>.
120</p>
121<p>You can declare variables with the enumeration type:
122</p>
123<div class="example">
124<pre class="example">enum emotion_state feelings_now;
125</pre></div>
126
127<p>In the C code itself, this is equivalent to declaring the variable
128<code>int</code>. (If all the enumeration values are positive, it is
129equivalent to <code>unsigned int</code>.) However, declaring it with the
130enumeration type has an advantage in debugging, because GDB knows it
131should display the current value of the variable using the
132corresponding name. If the variable&rsquo;s type is <code>int</code>, GDB can
133only show the value as a number.
134</p>
135<p>The identifier that follows <code>enum</code> is called a <em>type tag</em>
136since it distinguishes different enumeration types. Type tags are in
137a separate name space and belong to scopes like most other names in C.
138See <a href="Type-Tags.html">Type Tags</a>, for explanation.
139</p>
140<p>You can predeclare an <code>enum</code> type tag like a structure or union
141type tag, like this:
142</p>
143<div class="example">
144<pre class="example">enum foo;
145</pre></div>
146
147<p>The <code>enum</code> type is incomplete until you finish defining it.
148</p>
149<p>You can optionally include a trailing comma at the end of a list of
150enumeration values:
151</p>
152<div class="example">
153<pre class="example">enum emotion_state { neutral, happy, sad, worried,
154 calm, nervous, };
155</pre></div>
156
157<p>This is useful in some macro definitions, since it enables you to
158assemble the list of enumerators without knowing which one is last.
159The extra comma does not change the meaning of the enumeration in any
160way.
161</p>
162<hr>
163<div class="header">
164<p>
165Next: <a href="Defining-Typedef-Names.html" accesskey="n" rel="next">Defining Typedef Names</a>, Previous: <a href="Arrays.html" accesskey="p" rel="prev">Arrays</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
166</div>
167
168
169
170</body>
171</html>
Note: See TracBrowser for help on using the repository browser.