Enum in C Language
In C language, an enum
is a user-defined data type that consists of a set of named integer constants. An enum
declaration defines a new data type with a fixed set of possible values.
Here’s an example of an enum
declaration:
enum Color {
RED,
GREEN,
BLUE
};
In this example, we’ve defined a new enum
type called Color
with three possible values: RED
, GREEN
, and BLUE
. Each value corresponds to an integer constant, with RED
being assigned the value 0, GREEN
being assigned the value 1, and BLUE
being assigned the value 2.
You can use an enum
type to declare variables, like this:
enum Color favorite_color = GREEN;
In this example, we’ve declared a variable called favorite_color
with the enum
type Color
, and we’ve initialized it to the value GREEN
.
enum
types are often used to make code more readable and maintainable by giving names to values that might otherwise be represented by obscure integer constants.