An enumerated data type, or enum, is a user-defined data type in C that allows a programmer to create a set of named integer constants. Instead of using numbers directly, meaningful names can be used to represent related values.
An enumerated data type, or enum, is a user-defined data type in C that allows a programmer to create a set of named integer constants. Instead of using numbers directly, meaningful names can be used to represent related values.
Here, single, married,
divorced and widowed are named
enumeration constants.
An enum makes a program easier to understand because meaningful names can be used instead of unexplained integer values. It is useful when a variable can have one value from a fixed set of related choices.
Instead of storing marital status as 0,
1, 2 and 3, we can use
single, married,
divorced and widowed.
The enum keyword is followed by the enumeration name
and a list of named constants enclosed in braces.
After defining an enumeration type, a variable of that enum type can be declared. The variable can then store one of the defined enumeration values.
Here, today is an enum variable of type
Day.
By default, enumeration constants are assigned integer values
starting from 0 and increasing by 1.
The programmer can also explicitly assign values when required.
Here, the enumeration constants are assigned explicit integer values instead of the default sequence.
An enum variable can be assigned one of the named constants. Using these names makes the purpose of the value clear to the reader of the program.
The variable status is assigned the enumeration
constant married.
When values are not explicitly specified, the first enumeration
constant gets the value 0, the next gets
1, and so on. These are integer values internally,
but the named constants make the program easier to read.
The constants are assigned values in sequence:
Red = 0, Green = 1 and
Blue = 2.
| Advantage | Explanation |
|---|---|
| Readability | Meaningful names are easier to understand than raw numbers. |
| Better Organization | Related constant values can be grouped together. |
| Fixed Set of Values | Useful when a variable should represent one choice from a known set. |
| Maintainability | Named constants make programs easier to read and maintain. |
Write a C program using an enumerated data type to represent the status of an employee and display the selected status.
Create an enum named Status containing
single, married,
divorced and widowed.
Assign one of these values to an enum variable.
An enumeration provides names for a related set of integer constants. It is particularly useful when a variable can have one value from a small, predefined set of choices.
Employee Status = Married
In this program, married is one of the named constants
defined inside the enumeration. Internally, enumeration constants
are represented as integer values, but their names make the program
easier to understand.
Enumeration values are particularly useful with a
switch statement when different actions need to be
performed for different named choices.
Enumeration constants are names representing integer values. They
should not be confused with strings such as "married".
The enum constant married is not a string.
married → enumeration constant
"married" → string literal
| Concept | Remember |
|---|---|
| enum | User-defined type containing named integer constants. |
| Keyword |
enum
|
| Default Values | Start from 0 and increase by 1 unless explicitly assigned. |
| Use | Represents one choice from a fixed set of related values. |
| Example |
enum Status { single, married, divorced, widowed };
|
enum keyword?Watch a beginner-friendly explanation of enumerated data types in C programming.
A short handwritten-style revision sheet for enumerated data types will be provided here.
Use the mind map for quick revision of enum, constants, default values and practical uses.