Enumerated Types
Introduction to enumerated types in C
In C programming, an enumerated type is a user-defined data type that consists of a set of named integer constants. It is declared using the keyword enum.
Enumerated types make programs easier to read and maintain because meaningful names can be used instead of numeric values.
For example, instead of representing days using numbers such as 0, 1, and 2, we can use names such as SUNDAY, MONDAY, and TUESDAY.
Definition of an enumerated type
An enumerated type is a user-defined type in C that assigns names to a set of integer constant values. The keyword enum is used to declare it.Syntax of an enumerated type
enum enum_name
{
constant1,
constant2,
constant3
};
Explanation
- enum — keyword used to define an enumerated type.
- enum_name — name of the enumerated type.
- constant1, constant2, etc. — named integer constants called enumerators.
- The semicolon after the closing brace terminates the declaration.
For example:
enum day
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
};
By default, the enumerators receive the following values:
| Enumerator | Integer value |
|---|---|
| SUNDAY | 0 |
| MONDAY | 1 |
| TUESDAY | 2 |
| WEDNESDAY | 3 |
| THURSDAY | 4 |
| FRIDAY | 5 |
| SATURDAY | 6 |
The first enumerator receives the value 0 by default, and each subsequent enumerator receives the previous value plus one, unless a value is specified explicitly.
Declaring and using enum variables
After defining an enumerated type, you can declare variables of that type.
#include <stdio.h>
enum day
{
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY
};
int main( )
{
enum day today;
today = MONDAY;
printf("Day value = %d\n", today);
return 0;
}
Output:
Day value = 1
How does it work?
- enum day defines the enumerated type.
- today is a variable of type enum day.
today = MONDAY;assigns the value represented by MONDAY.- MONDAY has the integer value 1, so %d displays 1.
Important: printf("%d", today) displays the numeric value, not the word MONDAY. To display the name, use a separate mapping or switch statement.
Initialization of enumerated variables
An enumerated variable can be initialized at the time of declaration.
#include <stdio.h>
enum status
{
FAILURE,
SUCCESS,
PENDING
};
int main( )
{
enum status result = SUCCESS;
printf("%d\n", result);
return 0;
}
Output:
1
Here, FAILURE has value 0, SUCCESS has value 1, and PENDING has value 2.
Assigning explicit values to enumerators
You can specify integer constant values explicitly.
enum error_code
{
OK = 0,
NOT_FOUND = 404,
SERVER_ERROR = 500
};
The values are:
| Enumerator | Value |
|---|---|
| OK | 0 |
| NOT_FOUND | 404 |
| SERVER_ERROR | 500 |
Explicit values are useful when the values must correspond to predefined codes or statuses.
What happens when some values are assigned explicitly?
enum number
{
A = 5,
B,
C,
D = 10,
E
};
The values are:
- A = 5
- B = 6
- C = 7
- D = 10
- E = 11
When a value is omitted, the enumerator gets one more than the previous enumerator's value.
Example: Using enum with a switch statement
Enumerated types work particularly well with switch statements.
#include <stdio.h>
enum operation
{
ADD = 1,
SUBTRACT,
MULTIPLY,
DIVIDE
};
int main( )
{
enum operation choice = MULTIPLY;
int a = 10, b = 5;
switch (choice)
{
case ADD: printf("Result = %d\n", a + b);
break;
case SUBTRACT: printf("Result = %d\n", a - b);
break;
case MULTIPLY: printf("Result = %d\n", a * b);
break;
case DIVIDE: printf("Result = %d\n", a / b);
break;
}
return 0;
}
Output:
Result = 50
Here, MULTIPLY represents the value 3. The switch statement selects the multiplication case.
Example: Displaying enum names
An enumerator's name is not automatically printed by printf. A switch statement can translate its value into readable text.
#include <stdio.h>
enum traffic_light
{
RED,
YELLOW,
GREEN
};
int main( )
{
enum traffic_light light = GREEN;
switch (light)
{
case RED: printf("Red light");
break;
case YELLOW: printf("Yellow light");
break;
case GREEN: printf("Green light");
break;
}
return 0;
}
Output:
Green light
This makes the program more meaningful than simply displaying the number 2.
Using typedef with an enumerated type
The typedef keyword allows you to create a type alias. This can make enum declarations shorter.
Without typedef:
enum level
{
LOW,
MEDIUM,
HIGH
};
enum level current = HIGH;
With typedef:
typedef enum
{
LOW,
MEDIUM,
HIGH
} Level;
Level current = HIGH;
Both examples allow a variable to represent one of the named levels. The second form uses Level directly as the type name.
Advantages of enumerated types
- Improves readability: GREEN is more meaningful than 2.
- Simplifies maintenance: Names can make the purpose of values clearer.
- Reduces unexplained numbers: Avoids repeatedly using numeric codes throughout a program.
- Works well with switch: Useful for menus, states, and choices.
- Organizes related constants: Groups named values under one type declaration.
Limitations and important rules
- By default, the first enumerator has value 0.
- Enumerator names must be unique within their applicable scope.
- Enumerators are integer constant expressions.
- An enum variable is not automatically restricted at runtime to only the listed values; C permits values that do not correspond to a named enumerator.
- The exact integer type compatible with an enum is implementation-dependent, subject to the C standard and its constraints.
- Enumerators are not strings. For example, MONDAY does not automatically print as "MONDAY".
- enum improves clarity but does not validate user input by itself.
Difference between enum and #define
| Feature | enum | #define |
|---|---|---|
| Purpose | Defines named integer constants as part of an enumerated type | Defines a preprocessor macro |
| Example | enum { RED, GREEN, BLUE }; | #define RED 0 |
| Type association | Associated with an enum type | No C type is associated with the macro itself |
| Debugging/readability | Often provides clearer type-related information | Depends on the macro and debugging tools |
| Automatic numbering | Yes, when values are omitted | No, each value must be specified |
Quick revision
- enum is the keyword for an enumerated type.
- An enumerator is a named integer constant.
- By default, enumerators start at 0 and increase by 1.
- Explicit values can be assigned to enumerators.
- Enum variables can be used with switch statements.
- Use a mapping or switch statement when you want to display an enumerator's name.
- typedef can make enum declarations shorter.
Practice questions
- Define an enumerated type in C. Explain its declaration and initialization with examples.
- Explain default and explicit values in enumerated types.
- Write a C program that uses an enumerated type with a switch statement.
- Explain the advantages of enumerated types.
- Differentiate between enum and #define.