C Language and Libraries Help (clang.hlp) (
Table of Contents;
Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
enum
◄Example► ◄Up► ◄Contents► ◄Index► ◄Back►
─────C/C++ Language─────────────────────────────────────────────────────────
Keyword: enum
Syntax: enum [tag] {enum-list} [declarator];
enum tag declarator;
Summary: Specifies an enumerated type.
See also: class, struct
An enumerated type is a user-defined type consisting of a set of
named constants called enumerators. By default, the first
enumerator has a value of 0, and each successive enumerator is one
larger than the value of the previous one, unless you explicitly
specify a value for a particular enumerator. Enumerators do
not have to have unique values. The name of each enumerator is
treated as a constant and must be unique within the scope where
the enum is defined.
Example
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
} today; // Variable today has type Days
int tuesday; // Error, redefinition
// of tuesday
In C, you can use the enum keyword and the tag to declare
variables of the enumerated type. In C++, you can use the tag
alone. For example:
enum Days yesterday; // Legal in C and C++
Days tomorrow; // Legal in C++ only
yesterday = monday;
An enumerated type is an integral type. An enumerator can be
promoted to an integer value. However, converting an integer to an
enumerator requires an explicit cast, and the results are not
defined. For example:
int i = tuesday; // Legal; i = 2
yesterday = 0; // Error; no conversion
yesterday = (Days)0; // Legal, but results undefined
In C++, enumerators defined within a class are accessible only to
member functions of that class unless qualified with the class
name (for example, class_name::enumerator). You can use the same
syntax for explicit access to the type name (class_name::tag).
-♦-