Enumeration (Enum) in C
Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and provides meaningful names to these values. The primary use of enum in the C programming language is that it makes the program easy to understand and maintain. Enumeration is defined by the keyword ‘enum’. Enumeration consists of a set of some named integer constants.
It is also used to assign the names to the integral constants by which it makes a programmer or a developer easy to read and maintain the program.
In ANSI C, all the expressions that define the value of the enum constant have an integer type (int); that is, the storage which is associated with the enum variable, is the storage required for a single integer value (int). The enum constant or the value of the enum type can also be used everywhere in the C programming language as it permits an integer expression.
The enumeration type or enum type variables can be used while indexing the expressions and as the operands of all the preprocessor directives with the advantages that the values can be generated, and they obey the standard rules of scope.
Syntax
enum name_of_enum { constant 1, constant 2, … constant n };
In the above syntax, it can be seen that an enum has been defined as ‘name_of_enum’, which can represent any words or letter of the English language, and it contains ‘n’ constants.
The default value of ‘constant 1’ is 0, the default value of ‘constant 2’ is 1, and so on. This default value will be predefined, which can be changed during the time of declaration.
Variables of type enum can be defined in two different ways:
enum weekday { Monday, Tuesday, Wednesday }; enum week day; Or enum weekday { Monday, Tuesday, Wednesday } day;
E.g.:
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
The default value of Sunday is 0, Monday is 1, Tuesday is 2, Wednesday is 3, Thursday is 4, Friday is 5, and Saturday is 6. These default values can be changed during the time of declaration as shown below:
enum week { Sunday = 3, Monday = 1, Tuesday = 7, Wednesday = 2, Thursday = 4, Friday = 5, Saturday = 0 };
E.g 1: Default values
#include <stdio.h> #include <conio.h> enum color { blue, green, red, yellow, black }; int main() { /*a variable is initialized that will hold the enum values */ enum color present_color = blue; printf (“ The value of the color blue is = %d \n”, present_color); present_color = green; printf (“ The value of the color green is = %d \n”, present_color); present_color = red; printf (“ The value of the color red is = %d \n”, present_color); present_color = yellow; printf (“ The value of the color yellow is = %d \n”, present_color); present_color = black; printf (“ The value of the color black is = %d \n”, present_color); return 0; }
Output
E.g 2: Custom values to enum elements
#include <stdio.h> #include <conio.h> enum color { blue = 20, green = 50, red = 30, yellow = 10, black = 90 }; int main() { /*a variable is initialized that will hold the enum values */ enum color present_color = blue; printf (“ The value of the color blue is = %d \n”, present_color); present_color = green; printf (“ The value of the color green is = %d \n”, present_color); present_color = red; printf (“ The value of the color red is = %d \n”, present_color); present_color = yellow; printf (“ The value of the color yellow is = %d \n”, present_color); present_color = black; printf (“ The value of the color black is = %d \n”, present_color); return 0; }
Output
Points to be remember while initializing enum
- Two enum names can have the same value.
- If a programmer or a developer does not assign the values explicitly to the enum names, then the compiler has to assign the default values beginning from 0.
- A programmer can assign the values to the variables inside enum in any order he/she wishes to. All the unassigned names get the value as the value of the last name.
- The value assigned to the enum names must consist of some integral constant; that is, the value must always be in the range from a minimum possible integer to a maximum integer value.
- All the enumeration constant must be unique in their scope else the compiler will fail the compilation.