An enumeration, commonly called an enum, is a user-defined data type in C that allows us to create a set of named integer constants. Enums make programs easier to read because meaningful names can be used instead of numeric values.
0, and each following member normally increases by 1.
An enumeration is a user-defined type containing a collection of named integer constants.
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY
};
Here, MONDAY, TUESDAY, and
WEDNESDAY are enumeration constants.
Enums make code more readable and meaningful.
For example, instead of writing:
int day = 1;
we can write:
enum Day day = TUESDAY;
The second version clearly communicates what the value represents.
The basic syntax for defining an enumeration is:
enum EnumName
{
constant1,
constant2,
constant3
};
The enum definition ends with a semicolon.
enum Color
{
RED,
GREEN,
BLUE
};
This creates an enumeration type named enum Color.
After defining an enum, we can declare a variable of that enum type.
enum Color color;
Now the variable color can hold an enumeration value such as
RED, GREEN, or BLUE.
enum Color
{
RED,
GREEN,
BLUE
};
enum Color color;
color = GREEN;
The variable color now represents the named constant
GREEN.
If no values are explicitly assigned, enumeration constants normally start
from 0 and increase by 1.
enum Number
{
ZERO,
ONE,
TWO,
THREE
};
The values are normally:
We can assign specific integer values to enumeration constants.
enum Status
{
SUCCESS = 1,
ERROR = 2,
PENDING = 3
};
This can make the meaning of numeric status values clearer.
Some enum values can be assigned explicitly while others continue from the previous value.
enum Numbers
{
A = 10,
B,
C,
D = 20,
E
};
The values are:
Enums work well with switch statements.
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY
};
enum Day day = TUESDAY;
switch(day)
{
case MONDAY:
printf("Monday");
break;
case TUESDAY:
printf("Tuesday");
break;
case WEDNESDAY:
printf("Wednesday");
break;
}
An enum value can also be compared using an if statement.
enum Status
{
ACTIVE,
INACTIVE
};
enum Status status = ACTIVE;
if(status == ACTIVE)
{
printf("Account is active");
}
The names declared inside an enum represent integer constants.
enum Color
{
RED,
GREEN,
BLUE
};
Here, RED, GREEN, and BLUE are
enumeration constants.
Enum values can be printed as integers.
#include <stdio.h>
enum Color
{
RED,
GREEN,
BLUE
};
int main()
{
enum Color color = GREEN;
printf("%d", color);
return 0;
}
The output is normally 1 because GREEN is the
second constant.
A common approach is to read an integer and then map it to an enum value.
enum Choice
{
ADD = 1,
SUBTRACT = 2,
EXIT = 3
};
int choice;
printf("Enter choice: ");
scanf("%d", &choice);
if(choice == ADD)
{
printf("Addition selected");
}
The typedef keyword can give a shorter name to an enum type.
typedef enum
{
LOW,
MEDIUM,
HIGH
} Level;
Now we can declare a variable using:
Level level = HIGH;
#include <stdio.h>
typedef enum
{
OFF,
ON
} Power;
int main()
{
Power status = ON;
if(status == ON)
{
printf("Power is ON");
}
return 0;
}
enum Day
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
Enums are useful when a variable can have one value from a fixed set of named choices.
enum Month
{
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE
};
Starting January at 1 can make the values convenient for
applications that use month numbers.
An enum can be used as a member of a structure.
enum Gender
{
MALE,
FEMALE,
OTHER
};
struct Student
{
int id;
enum Gender gender;
};
This allows the structure to store a named category.
An enum can be passed to a function like other C values.
enum Status
{
ACTIVE,
INACTIVE
};
void displayStatus(enum Status status)
{
if(status == ACTIVE)
{
printf("Active");
}
else
{
printf("Inactive");
}
}
A pointer can store the address of an enum variable.
enum Color
{
RED,
GREEN,
BLUE
};
enum Color color = BLUE;
enum Color *ptr = &color;
printf("%d", *ptr);
The dereference operator * accesses the value stored at the
address.
An array can contain enum values.
enum Color
{
RED,
GREEN,
BLUE
};
enum Color colors[3] =
{
RED,
GREEN,
BLUE
};
Each array element stores an enum value.
enum Menu
{
ADD = 1,
VIEW,
UPDATE,
DELETE,
EXIT
};
This is useful for programs that contain menus with fixed choices.
enum Menu choice = VIEW;
enum TrafficLight
{
RED,
YELLOW,
GREEN
};
enum TrafficLight signal = GREEN;
if(signal == GREEN)
{
printf("Go");
}
Enums can represent states with a small, fixed set of meaningful names.
Enumerator names share the same identifier namespace as other ordinary identifiers at the same scope. Therefore, the same enumerator name should not be declared again in a conflicting scope.
enum First
{
VALUE
};
/* Avoid declaring another VALUE
in the same scope. */
The enum type name and the enumerator names are different things.
enum Color
{
RED,
GREEN,
BLUE
};
Here:
Color is the enum type name.RED is an enumerator.GREEN is an enumerator.BLUE is an enumerator.#include <stdio.h>
enum Operation
{
ADD = 1,
SUBTRACT,
MULTIPLY,
DIVIDE
};
int main()
{
enum Operation operation = MULTIPLY;
switch(operation)
{
case ADD:
printf("Addition");
break;
case SUBTRACT:
printf("Subtraction");
break;
case MULTIPLY:
printf("Multiplication");
break;
case DIVIDE:
printf("Division");
break;
}
return 0;
}
Enums improve readability by replacing unexplained numbers with meaningful names.
Without enum:
int status = 2;
With enum:
enum Status
{
PENDING = 1,
APPROVED = 2,
REJECTED = 3
};
enum Status status = APPROVED;
The second version makes the meaning of the value easier to understand.
Enums are commonly useful for representing:
Create a C program that:
Course.C, CPP, JAVA, and PYTHON.This practice will help you understand enum definitions, variables, comparisons, and switch statements.
if and switch statements.typedef can provide a shorter name for an enum type.Question: What is the default value of the first enumerator when no explicit value is assigned?