What can I do with an enum variable?

When I declare a enum variable like this:

enum paint_colors { RED, GREEN, BLUE, ...} colors;

is the colors variable useful? If so, what can I do with it?

Thanks in advance.

Internally, an enum is treated as an integer that can only hold a limited range of values. In this case, the constants RED, GREEN, BLUE, ... will be defined and will be equal to 0, 1, 2, ... (respectively). The variable colors can be used anywhere an int can be used. You can use operators like ++ to iterate through the list of colors. The only difference between declaring enum paint_colors colors and int colors is that the enumerated variable can should only be assigned one of the enumerated constants.

This gives you several benefits over using #define to create a series of constants and using a normal int for colors. First, some debuggers can detect that colors is an enumerated type and will display the name of the enumerated constant instead of a numeric value.

More importantly, this can add an additional layer of type checking. It is not required by the C standard, but some compilers check and make sure that values assigned to a variable of enumerated type correspond to one of the enumerated constants.

Mentally, you can almost think of this is similar to saying:

#define RED    0
#define GREEN  1
#define BLUE   2
typedef int paint_colors;
paint_colors colors;

The variable is treated like an int, but explicitly giving it a different type helps to clarify what the variable is and what it is used for.

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain. enum State {Working = 1, Failed = 0}; The keyword ‘enum’ is used to declare new enumeration types in C and C++.

Really what you're doing there is declaring a variable inline with the rest of the definition of the enumeration. It's equivalent to:

enum paint_colors { RED, GREEN, BLUE };
enum paint_colors colors;

Often, you'll see a typedef associated with the definition:

typedef enum _paint_colors { RED, GREEN, BLUE } paint_colors;

Which lets you use the enumeration more like the built in types:

paint_colors color;

So the answer to your question is that colors is a variable of type enum paint_colors, and can be used any way you think is appropriate for such a variable:

colors = RED;
if (colors != GREEN)
{
    colors = BLUE;
}

And so on.

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

You could paint a roller coaster!

typedef struct {
  Color car;
  Color door_handle;
  Color wheels;
} RollerCoasterColors;

int main(void) {
  RollerCoasterColors roller_coaster_colors;
  roller_coaster_colors.car = RED;
  roller_coaster_colors.door_handle = YELLOW;
  roller_coaster_colors.wheels = PURPLE;

  // now paint_roller_coaster() knows what colour to paint each part!
  paint_roller_coaster(roller_coaster_colors);
}

Enumeration (enum) is a user-defined datatype (same as structure). It consists of various elements of that type. There is no such specific use of enum, we use it just to make our codes neat and more readable. We can write C programs without using enumerations also.

In C, the enum type variable can be iterated, such as ++, +; But this is not allowed in C++;

enum WEEKDAY {MON, TUE, WED, THU, FRI, SAT, SUN, END};
for(WEEKDAY day = 0; day < END; day++){
    printf("day index = %d\n", day);
}

can be compiled in C, but not in C++;

Introduction An Enum (short for Enumerated) variable type is a special flavor of a Long type variable. With an Enum, you can specify a number of valid values for that variable and descriptive constant names for the values of the Enum. These values are used instead of constants.

An enum is helpful when you have a given set of possible values, where each value has a meaningful name. Elsewhere, you refer to the enum values, rather than "magic numbers", which can become meaningless among other code.

The Java enum type provides a language-supported way to create and use constant values. By defining a finite set of values, the enum is more type-safe than constant literal variables like String or int. However, enum values are required to be valid identifiers, and we're encouraged to use SCREAMING_SNAKE_CASE by convention.

In C#, enum is a value type data type. The enum is used to declare a list of named integer constants. It can be defined using the enumkeyword directly inside a namespace, class, or structure. The enum is used to give a name to each constant so that the constant integer can be referred using its name. Example: enum.

Short for enumeration, an enum variable typecan be found in C (ANSI, not the original K&R), C++ and C#. The idea is that instead of using an intto represent a set of values, a type with a restricted set of values is used instead. For example, if we use the colors of the rainbow, which are

An enumeration is a set of named integer constants. An enumerated type is declared using the enum keyword. C# enumerations are value data type. In other words, enumeration contains its own values and cannot inherit or cannot pass inheritance.

Comments
  • @drigoSkalWalker, I fixed up your syntax for you. I hope I understood the question correctly.
  • I have downvoted many answers now. Fast guns out there, read first, fire later. He is not asking about enum values, but about enum variable. All answers but the first one completely ignore this.
  • it continue useless for me, and for C. I can use int or other types instead paint_color type, the variable colors is still useless.
  • @drigoSkalWalker, it lets you do some limited typechecking. If you don't want to use them, you don't have to. It can come in handy though.
  • You are asking two questions now. Whether the variable 'colors' is useful, depends on the rest of the program. The type 'enum paint_colors' is useful as it communicates the intent and value range for this variable and others - it puts more emphasis on the What ('this is one of 3 colors'), and less on the How ('this is stored as an int').
  • That doesn't use the colors variable at all, which is what the OP was asking about.
  • That's not what the OP is asking, I think. He wants to know what colors is good for.