×

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

Enumeration (Enum) In C

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

Enumeration (Enum) In C

Points to be remember while initializing enum

  1. Two enum names can have the same value.
  2. 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.
  3. 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.
  4. 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.
  5. All the enumeration constant must be unique in their scope else the compiler will fail the compilation.

Related Topics

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare...

2 minutes read.

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how...

4 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

Hook() function in C

Use of hook () function in C Introduction:  The hook () is a function used in the C programming language. The basic concept of the hook() function is that it can remove the function...

3 minutes read.

How to delete a file in C

A file is a group of data kept on a secondary device, such as a hard disc. It is typically utilized as a real-world application with a lot of data. These...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

How to Calculate Time Complexity in C?

What is time complexity? An algorithm's time complexity measures how long it takes to complete a task in relation to the size of the input. It should be noted that the...

5 minutes read.

Simple Programs in C Language

In this article, we will discuss the basic programs in C language. Addition of two values in C. Swapping two values. Finding the odd or even values. Finding vowels and consonants in C. Finding the...

11 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.