Académique Documents
Professionnel Documents
Culture Documents
C Enumeration Declarations
Visual Studio 2005
Variables of enum type c an be used in indexing expressions and as operands of all arithmetic and
relational operators. Enumerations provide an alternative to the #define preprocessor directive
with the advantages that the values can be generated for you and obey normal scoping rules.
In ANSI C, the expressions that define the value of an enumerator constant always have int type;
thus, the storage associated with an enumeration variable is the storage required for a single int
value. An enumeration c onstant or a value of enumerated type can be used anywhere the C
language permits an integer expression.
Syntax
enum-spec ifier:
enum identifier
The optional identifier names the enumeration type defined by enumerator-list. This identifier is
often c alled the "tag" of the enumeration specified by the list. A type specifier of the form
declares identifier to be the tag of the enumeration spec ified by the enumerator-list
nonterminal. The enumerator-list defines the "enumerator content." The enumerator-list is
described in detail below.
If the declaration of a tag is visible, subsequent declarations that use the tag but omit
enumerator-list specify the previously declared enumerated type. The tag must refer to a
defined enumeration type, and that enumeration type must be in c urrent scope. Since the
enumeration type is defined elsewhere, the enumerator-list does not appear in this dec laration.
Dec larations of types derived from enumerations and typedef declarations for enumeration
Microsoft is conducting an online survey to understand your
types can use the enumeration tag before the enumeration
opinion type
of the MSDN is defined.
Web site. If you choose to participate,
the online survey will be presented to you when you leave
the MSDN Web site.
Syntax
Would you like to participate?
enumerator-list:
Yes No
enumerator
Privacy Statement
enumerator-list , enumerator
enumerator:
enumeration-constant
enumeration-constant = c onstant-expression
…microsoft.com/…/whbyts4t(v=vs.80).… 1/3
5/1/2011 C Enumeration Declarations (C)
enumeration-constant:
identifier
An enumeration set c an c ontain duplic ate c onstant values. For example, you c ould
assoc iate the value 0 with two different identifiers, perhaps named null and zero, in the
same set.
The identifiers in the enumeration list must be distinct from other identifiers in the same
sc ope with the same visibility, including ordinary variable names and identifiers in other
enumeration lists.
Enumeration tags obey the normal scoping rules. They must be distinct from other
enumeration, struc ture, and union tags with the same visibility.
Examples
These examples illustrate enumeration dec larations:
The value 0 is assoc iated with saturday by default. The identifier sunday is explicitly set to 0.
The remaining identifiers are given the values 1 through 5 by default.
In this example, a value from the set DAY is assigned to the variable today.
Note that the name of the enumeration c onstant is used to assign the value. Since the DAY
enumeration type was previously declared, only the enumeration tag DAY is necessary.
To explicitly assign an integer value to a variable of an enumerated data type, use a type cast:
…microsoft.com/…/whbyts4t(v=vs.80).… 2/3
5/1/2011 C Enumeration Declarations (C)
enum BOOLEAN /* Declares an enumeration data type called BOOLEAN */
{
false, /* false = 0, true = 1 */
true
};
or as
if ( match_flag == false )
{
.
. /* statement */
.
}
end_flag = true;
Unnamed enumerator data types c an also be dec lared. The name of the data type is omitted,
but variables can be declared. The variable response is a variable of the type defined:
See Also
Refer ence
Community Content
…microsoft.com/…/whbyts4t(v=vs.80).… 3/3