Loading content...
Loading content...
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.
Enums provide meaningful names to integer values, making code easier to understand.
if(status == 1)
{
printf("Success");
}Here, it is not immediately clear what value 1 represents.
if(status == SUCCESS)
{
printf("Success");
}This code is much easier to read.
An enum is declared using the enum keyword.
enum enum_name
{
n1,
n2,
n3
};Each identifier is automatically assigned an integer value.
enum Calculate
{
SUM,
DIFFERENCE,
PRODUCT,
QUOTIENT
};By default:
text
SUM = 0
DIFFERENCE = 1
PRODUCT = 2
QUOTIENT = 3Each subsequent value is incremented by 1.
After defining an enum, variables can be created using the enum name.
enum enum_name variable_name;enum Calculate operation;An enum variable can be initialized using enum constants.
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir = NORTH;
printf("%d", dir);
return 0;
}text
1Because:
text
EAST = 0
NORTH = 1
WEST = 2
SOUTH = 3Enum variables can also be assigned integer values directly.
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir;
dir = 3;
printf("%d", dir);
return 0;
}text
3Although this is valid, it is generally not recommended because it reduces code readability.
We can assign custom integer values to enum constants.
enum enum_name
{
n1 = value1,
n2 = value2,
n3
};#include <stdio.h>
enum Numbers
{
A = 3,
B = 2,
C
};
int main()
{
printf("%d %d %d", A, B, C);
return 0;
}text
3 2 3text
A = 3
B = 2
C = 3Since C was declared after B, it receives the next value after B.
Unlike variable names, enum constants can have the same value.
enum Demo
{
A = 1,
B = 1,
C = 2
};This is perfectly valid.
Enum names follow the same rules as variable names.
enum Direction
enum Days
enum StudentStatusenum 1Direction
enum floatWithin the same scope, enum constant names must be unique.
enum Calculate
{
SUM,
PRODUCT
};
enum Item
{
PRODUCT,
SERVICE
};text
error: redeclaration of enumerator 'PRODUCT'Because PRODUCT is declared twice in the same scope.
Memory is allocated only when an enum variable is created.
Usually, an enum is stored as an integer.
#include <stdio.h>
enum Direction
{
EAST,
NORTH,
WEST,
SOUTH
};
int main()
{
enum Direction dir = NORTH;
printf("%zu bytes", sizeof(dir));
return 0;
}text
4 bytesMost compilers store enums as integers, but the actual size may vary depending on the compiler.
The typedef keyword can create an alias for an enum.
This avoids repeatedly writing the enum keyword.
#include <stdio.h>
typedef enum
{
EAST,
NORTH,
WEST,
SOUTH
} Direction;
int main()
{
Direction dir = NORTH;
printf("%d", dir);
return 0;
}text
1Now we can simply write:
Direction dir;instead of:
enum Direction dir;Enums are commonly used with switch statements.
#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;
}text
TuesdayMakes 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.
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.
Enums are often used to represent different states of a system.
enum State
{
START,
RUNNING,
STOPPED
};Enums provide meaningful names for error values.
enum ErrorCode
{
SUCCESS,
FILE_NOT_FOUND,
ACCESS_DENIED
};Enums can represent menu choices.
enum Menu
{
ADD,
DELETE,
UPDATE,
EXIT
};enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};enum Permission
{
READ,
WRITE,
EXECUTE
};#define SUCCESS 0
#define ERROR 1enum Status
{
SUCCESS,
ERROR
};Enum is generally preferred because:
It groups related constants together.
It improves readability.
It provides better type checking.
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.
typedef can simplify enum declarations.
Enums improve code readability and maintainability.
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.