source: doc/theses/jiada_liang_MMath/background.tex @ 9262fe9

Last change on this file since 9262fe9 was 7bb516f, checked in by Peter A. Buhr <pabuhr@…>, 8 months ago

more proofreading on enumeration thesis

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[18ebc28]1\chapter{Background}
[956299b]2
3
4\section{C-Style Enum}
5
6The C-Style enumeration has the following syntax and semantics.
7\begin{cfa}
8enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday };
9\end{cfa}
10Enumerators without an explicitly designated constant value are \Newterm{auto-initialized} by the compiler: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@.
11For example, @Monday@ to @Wednesday@ are implicitly assigned with constants @0@--@2@, @Thursday@ is explicitly set to constant @10@, and @Friday@ to @Sunday@ are implicitly assigned with constants @11@--@13@.
12Initialization may occur in any order.
13\begin{cfa}
14enum Weekday { Thursday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday };
15\end{cfa}
16Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.
17\begin{cfa}
18enum Weekday {
19        Thursday = 10, Friday, Saturday, Sunday,
20        Monday = 0, Tuesday, Wednesday@,@ // terminating comma
21};
22\end{cfa}
23This feature allow enumerator lines to be interchanged without moving a comma.\footnote{
[f9da761]24A terminating comma appears in other C syntax, \eg the initializer list.}
25Finally, C enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.
[956299b]26
27In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values.
28In practice, since integral constants are used, which have type @int@ (unless qualified with a size suffix), C uses @int@ as the underlying type for enumeration variables.
[7bb516f]29Finally, there is an implicit bidirectional conversion between an enumeration and its integral type.
[956299b]30\begin{cfa}
31{
32        enum Weekday { /* as above */ };        $\C{// enumerators implicitly projected into local scope}$
33        Weekday weekday = Monday;                       $\C{// weekday == 0}$
34        weekday = Friday;                                       $\C{// weekday == 11}$
35        int i = Sunday;                                         $\C{// implicit conversion to int, i == 13}$
36        weekday = 10000;                                        $\C{// UNDEFINED! implicit conversion to Weekday}$
37}
38int j = Wednesday;                                              $\C{// ERROR! Wednesday is not declared in this scope}$
39\end{cfa}
40The implicit conversion from @int@ to an enumeration type is an unnecessary source of error.
41
42It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration.
43\begin{cfa}
44#define Monday 0
45static const int Monday = 0;
46enum { Monday };
47\end{cfa}
48For @#define@, the programmer has to play compiler and explicitly manage the enumeration values;
49furthermore, these are independent constants outside of any language type mechanism.
50The same explicit management is true for @const@ declarations, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{
51C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} and immediate operands of assembler instructions.
52Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations.
Note: See TracBrowser for help on using the repository browser.