Enumeration (enum) Tutorials
Programming
Enumeration (enum) in C
An Enumeration (enum) is a user-defined data type in C that allows us to create a set of named integer constants.
Enums make programs more readable and maintainable by replacing numeric values with meaningful names.
Instead of writing numbers such as 0, 1, or 2 directly in the code, we can use names like EAST, WEST, SUCCESS, or ERROR.
Why Use Enums?
Enums provide meaningful names to integer values, making code easier to understand.
Without Enum
if(status == 1)
{
printf("Success");
}Here, it is not immediately clear what value 1 represents.
With Enum
if(status == SUCCESS)
{
printf("Success");
}This code is much easier to read.
Enum Declaration
An enum is declared using the enum keyword.
Syntax
enum enum_name
{
n1,
n2,
n3
};Each identifier is automatically assigned an integer value.
Example of Enum
enum Calculate
{
SUM,
DIFFERENCE,
PRODUCT,
QUOTIENT
};By default:
text
SUM = 0
DIFFERENCE = 1
PRODUCT = 2
QUOTIENT = 3Each subsequent value is incremented by 1.
Creating Enum Variables
After defining an enum, variables can be created using the enum name.
Syntax
enum enum_name variable_name;Example
enum Calculate operation;Initializing Enum Variables
An enum variable can be initialized using enum constants.
Example
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir = NORTH;
printf("%d", dir);
return 0;
}Output
text
1Because:
text
EAST = 0
NORTH = 1
WEST = 2
SOUTH = 3Assigning Integer Values
Enum variables can also be assigned integer values directly.
Example
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir;
dir = 3;
printf("%d", dir);
return 0;
}Output
text
3Although this is valid, it is generally not recommended because it reduces code readability.
Assigning Values Manually
We can assign custom integer values to enum constants.
Syntax
enum enum_name
{
n1 = value1,
n2 = value2,
n3
};Example
#include <stdio.h>
enum Numbers
{
A = 3,
B = 2,
C
};
int main()
{
printf("%d %d %d", A, B, C);
return 0;
}Output
text
3 2 3Explanation
text
A = 3
B = 2
C = 3Since C was declared after B, it receives the next value after B.
Enum Values Can Be Duplicated
Unlike variable names, enum constants can have the same value.
Example
enum Demo
{
A = 1,
B = 1,
C = 2
};This is perfectly valid.
Enum Naming Rules
Enum names follow the same rules as variable names.
Valid Names
enum Direction
enum Days
enum StudentStatusInvalid Names
enum 1Direction
enum floatEnum Constants Must Be Unique
Within the same scope, enum constant names must be unique.
Example
enum Calculate
{
SUM,
PRODUCT
};
enum Item
{
PRODUCT,
SERVICE
};Output
text
error: redeclaration of enumerator 'PRODUCT'Because PRODUCT is declared twice in the same scope.
Size of Enum
Memory is allocated only when an enum variable is created.
Usually, an enum is stored as an integer.
Example
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir = NORTH;
printf("%zu bytes", sizeof(dir));
return 0;
}Output
text
4 bytesMost compilers store enums as integers, but the actual size may vary depending on the compiler.
Enum with Typedef
The typedef keyword can create an alias for an enum.
This avoids repeatedly writing the enum keyword.
Example
#include <stdio.h>
typedef enum
{
EAST,
NORTH,
WEST,
SOUTH
} Direction;
int main()
{
Direction dir = NORTH;
printf("%d", dir);
return 0;
}Output
text
1Now we can simply write:
Direction dir;instead of:
enum Direction dir;Enum in Switch Statement
Enums are commonly used with switch statements.
Example
#include <stdio.h>
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY
};
int main()
{
enum Day today = TUESDAY;
switch(today)
{
case MONDAY:
printf("Monday");
break;
case TUESDAY:
printf("Tuesday");
break;
case WEDNESDAY:
printf("Wednesday");
break;
}
return 0;
}Output
text
TuesdayAdvantages of Enum
Makes code easier to read.
Improves code maintainability.
Eliminates the use of magic numbers.
Helps organize related constants.
Works well with switch statements.
Reduces programming errors.
Disadvantages of Enum
Enum values are integers only.
Names must be unique within the same scope.
Cannot directly store strings or floating-point values.
Limited flexibility compared to structures.
Applications of Enum
State Representation
Enums are often used to represent different states of a system.
Example
enum State
{
START,
RUNNING,
STOPPED
};Error Codes
Enums provide meaningful names for error values.
Example
enum ErrorCode
{
SUCCESS,
FILE_NOT_FOUND,
ACCESS_DENIED
};Menu Options
Enums can represent menu choices.
Example
enum Menu
{
ADD,
DELETE,
UPDATE,
EXIT
};Days of the Week
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};File Permissions
enum Permission
{
READ,
WRITE,
EXECUTE
};Difference Between Enum and Macros
Macro
#define SUCCESS 0
#define ERROR 1Enum
enum Status
{
SUCCESS,
ERROR
};Enum is generally preferred because:
It groups related constants together.
It improves readability.
It provides better type checking.
Key Points
Enum is a user-defined data type.
Enum constants are integer values.
By default, the first constant gets value 0.
Subsequent constants are incremented automatically.
Custom values can be assigned manually.
Enum variables can store enum constants.
typedefcan simplify enum declarations.Enums improve code readability and maintainability.
Summary
An enumeration (enum) is a user-defined data type used to create named integer constants. It helps replace numeric values with meaningful names, making programs easier to read, understand, and maintain. Enums are widely used for states, menu options, error codes, file permissions, and many other real-world programming scenarios.