I've heard before that I should simply let the compiler choose which values to assign for enumerated constants if I'm not doing something clever like using the values as bitmasks. If I'm just using enumeration values for more explicit code documentation, are there any gotchas that could creep in if I don't explicitly define all the values? I believe that values are assigned in ascending order. Should I define the 1st value to ensure the same values for each successive compilation?
The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally, a closing brace and a semicolon. Here's an example: enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };
A Java Enum is a Java type-class used to define collections of constants for your software according to your own needs. Each item in a Java enum is called a constant, an immutable variable — a value that cannot be changed.
Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.
An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.
From C99, Section 6.7.2.2p3:
The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.)
The only time you need to assign a value to the first enumerator is if you want its value to be different from 0
. Usually, you wouldn't do that, since the enumerators are often used to index an array.
Below is a simple example of "adding 1 to the value of the previous".
enum {
A,
B,
C = 0,
D,
E
};
int main ()
{
printf("%d\n", A);
printf("%d\n", B);
printf("%d\n", C);
printf("%d\n", D);
printf("%d\n", E);
return 0;
}
The output for the above program is:
0
1
0
1
2
The only "gotcha" that I can think of is that if you want multiple enum
names to represent the same value, you should remember to either stick them right next to the item you alias, or at the bottom of the enum
list. I prefer the latter so I can count the enum
lines to predict what the enum
values will be.
enum {
A,
B,
E,
/* aliases below */
C = A,
D = B
};
Otherwise, an enumerator is just like an int
literal.
I think it will depend on your use of the type.
Advantages to not providing enum
initializers:
Advantages to providing enum
initializers:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With