×

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

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

C vs Java Strings

String in C In C, we can define a string as a bunch of characters. A character array is distinguished from a string by the presence of the special character '\0'...

5 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Find out Power without using POW function in C

Introduction: In this discussion, we will discuss how we find out power without using the POW function in C. In this article, you'll understand how to compute integer powers in...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 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.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Comments in C

Comments are used to comment on the line of code in the program. Comments are a way of inserting remarks and reminders into code without affecting its behavior. The compiler...

1 minute read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.