Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Enumerated type==== The [[enumerated type]] in C, specified with the {{code|enum}} keyword, and often just called an "enum" (usually pronounced {{IPAc-en|'|i:|n|V|m}} {{respell|EE|num}} or {{IPAc-en|'|i:|n|u:|m}} {{respell|EE|noom}}), is a type designed to represent values across a series of named constants. Each of the enumerated constants has type {{code|int}}. Each {{code|enum}} type itself is compatible with {{code|char}} or a signed or unsigned integer type, but each implementation defines its own rules for choosing a type. Some compilers warn if an object with enumerated type is assigned a value that is not one of its constants. However, such an object can be assigned any values in the range of their compatible type, and {{code|enum}} constants can be used anywhere an integer is expected. For this reason, {{code|enum}} values are often used in place of preprocessor {{code|#define}} directives to create named constants. Such constants are generally safer to use than macros, since they reside within a specific identifier namespace. An enumerated type is declared with the {{code|enum}} specifier and an optional name (or ''tag'') for the enum, followed by a list of one or more constants contained within curly braces and separated by commas, and an optional list of variable names. Subsequent references to a specific enumerated type use the {{code|enum}} keyword and the name of the enum. By default, the first constant in an enumeration is assigned the value zero, and each subsequent value is incremented by one over the previous constant. Specific values may also be assigned to constants in the declaration, and any subsequent constants without specific values will be given incremented values from that point onward. For example, consider the following declaration: <syntaxhighlight lang=C>enum colors { RED, GREEN, BLUE = 5, YELLOW } paint_color;</syntaxhighlight> This declares the {{code|enum colors}} type; the {{code|int}} constants {{code|RED}} (whose value is 0), {{code|GREEN}} (whose value is one greater than {{code|RED}}, 1), {{code|BLUE}} (whose value is the given value, 5), and {{code|YELLOW}} (whose value is one greater than {{code|BLUE}}, 6); and the {{code|enum colors}} variable {{code|paint_color}}. The constants may be used outside of the context of the {{code|enum}} (where any integer value is allowed), and values other than the constants may be assigned to {{code|paint_color}}, or any other variable of type {{code|enum colors}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)