enum

From Appmethod Topics
Jump to: navigation, search

Go Up to Keywords, Alphabetical Listing Index


Category

Type Specifiers

Syntax

 [<type_tag>] {<constant_name> [= <value>], ...} [var_list];
  • <type_tag> is an optional type tag that names the set.
  • <constant_name> is the name of a constant that can optionally be assigned the value of <value>. These are also called enumeration constants.
  • <value> must be an integer. If <value> is missing, it is assumed to be: <prev> + 1 where <prev> is the value of the previous integer constant in the list. For the first integer constant in the list, the default value is 0.
  • <var_list> is an optional variable list that assigns variables to the enum type.

Description

Use the enum keyword to define a set of constants of type int, called an enumeration data type.

An enumeration data type provides mnemonic identifiers for a set of integer values. Use the -b flag to toggle the Treat Enums As Ints option. .Enums are always interpreted as ints if the range of values permits this, but if they are not ints the value gets promoted to an int in expressions. Depending on the values of the enumerators, identifiers in an enumerator list are implicitly of type signed char, unsigned char, short, unsigned short, int, or unsigned int.

In C, an enumerated variable can be assigned any value of type int--no type checking beyond that is enforced. In C++, an enumerated variable can be assigned only one of its enumerators.

In C++, you can omit the enum keyword if <tag_type> is not the name of anything else in the same scope. You can also omit <tag_type> if no further variables of this enum type are required.

In the absence of a <value> the first enumerator is assigned the value of zero. Any subsequent names without initializers will then increase by one. <value> can be any expression yielding a positive or negative integer value (after possible integer promotions). These values are usually unique, but duplicates are legal.

Enumeration tags share the same name space as structure and union tags. Enumerators share the same name space as ordinary variable identifiers.

In C++, enumerators declared within a class are in the scope of that class.