Q

Emertxe-News & Blog

Home » Blog » Enumeration in C
Enumeration in C

Introduction:

Let us understand what a datatype is, before we start learning about Enumeration in C. Datatype in any programming language is the collection or group of data with the values which has fixed characteristics. There are three different datatypes generally in ANSI C. They are primary, derived and user defined datatypes. Primary or built-in datatype includes int,char,double,float and void. Secondary or derived datatype includes array,references, pointers whereas user defined datatype can include Structure, Enumeration and Union.

Let us learn about Enum as a part of this blog post.

Enumeration in C:

Enumeration is a user defined datatype in C. It is used to assign names to integral constant and to define an enumeration, keyword enum is used.

Syntax of enum is,
enum {constant1, constant2,………….,constantN};
Different types of declaration is possible in enum.

Type 1:

enum colors {
red,
blue,
orange };

Type 2:

enum bool {false,true};
enum bool var;
(or)
enum bool {
false, true
}var;

Now that we have seen the different types of declarations of variables using enum, we will go through various types of initialization of enum elements.

Initializations:

Example 1:

User can assign values to enum names explicitly while declaring them. If not assigned, compiler will assign values to the enum constants from 0.

#include <stdio.h>
enum datatypes {
integer,
character,
floating_point
};

int main()
{
   enum datatypes type = floating_point;
   printf("The type number stored in types is %dn", type);
   printf("n");
   return 0;
}

Output:

 

In the above given example, values to the enum names are not explicitly assigned. Hence when we try to print floating_point in line 11, the value printed is 2. Thus, compiler has assigned values to the constants as follows, Integer = 0, character = 1, floating_point = 2.

Example 2:

User can assign any value to any names in any order. The name for which value is not assigned will take the value of its previous name added by 1.

#include <stdio.h>
enum datatypes {
integer=10,
character,
floating_point=10,
long_integer = 25
};

int main()
{
   printf("The values are integer = %dn character = %dn floating_point = %dn long_integer = %d",integer,character,floating_point,long_integer);
   return 0;
}

Output:

In the above given example, enum constant character in line 4 takes value of integer in line 3 added by 1. Hence when we print character we get the value as 11. Also if we observe the values assigned to integer and floating point in line 3 and line 5, it is 10. Hence we can have same value assigned to two different names.

Example 3:

Any enum constant should be defined/declared once in a file, it means the constant should be unique in their scope. User cannot define same name in multiple enums under a same file.

#include <stdio.h>
enum datatypes {
integer,
character
};
 
enum datatypes1 {
long_integer,
character
};
 
int main()
{
  printf("value of character = %d", character);
  return 0;
}

In the above given example, character declared in two different enum under same file. Hence compiler flags an error saying character has been defined twice.

Enum vs Macro

Programmer can improve the readability of code using Enums. Here is an example which explains it completely.
Consider the below macro declaration where 3 different macros are declared with values 0,1 and 2.

Example:

#define Sunday 0
#define Monday 1
#define Tuesday 2
 
typedef enum {
Sunday,
   Monday,
   Tuesday
   }days;
 
void set_calender_to_notify(days wd);  
 
void set_calender_to_notify(int wd);

It is easier to read a code like one written in line 11 rather than the code in line 13. It is because in a big program, where there are more number of lines of code and if we use enum we will get to know what can be passed to argument parameters. Also, we don’t have to explicitly assign any values to enum constants during declaration like Macros.

Though readability is achieved using enums, we cannot define some values using enums. Using macros, we can define PI as 3.14. If the same is done using enums by providing values explicitly, compiler throws an error.

Scope rule:

Enum follows scope rule unlike macros. Hence if any change is made to enum constants within a scope, it will not be reflected outside the scope. Here in the below given example, compiler assigns integer 1 to enumerator true_val in line 5. When the value is explicitly assigned to true_val within a scope in line 13, we could observe that the value is reflected in the print statement which is present within the scope (line 14), not in line 16. This is helpful when we write a project which contains more number of codes, where if we need to change value of an enumerator, we can have the luxury of changing it, since it won’t be reflected anywhere else other than that particular scope.

Example:

#include <stdio.h>
 
typedef enum {
   error_val,
   true_val,
   false_val
  }flag;
 
  int main()
  {
  if (1)
  {
       flag true_val = 100;
       printf("value of enum constant within scope %dn",true_val);
   }
   printf("value of enum constant outside scope %dn",true_val);
   return 0;
}

Enum and Switch Statement:

Enum constants can be used in switch statements by providing the symbolic names to case identifiers.

Example:

#include <stdio.h>
  
   typedef enum {
   error_val,
   true_val,
   false_val
    }flag;
 
   int main()
  {
   flag flags = true_val;
 
   switch (flags)
   {
         case error_val:
         printf("Error messagen");
         return 0;
 
         case true_val:
         printf("Condition is Truen");
         return 1;
 
         case false_val:
           printf("Condition is Falsen");
         return 2;
 
         default:
         printf("Provide valid case identifiern");
         break;
   }
 }

Output:

In the above given example, we could observe that the case identifiers in line 15,19 and 23 are enum constants. When any one of the cases becomes true, the respective print statement executed. Since we have passed true_val to flags in line 11, the print statement under case true_val (line 20) got executed.

Conclusion:

Enum being the user defined data type which consists of integral constants, is very helpful in increasing the code readability, thus decreasing the time taken for understanding the code.

Though enum increases readability, we can’t use enums for bitwise operations where an unsigned type is required. It is because enums are signed and they work well when user doesn’t have to care about the actual value of it.

YOU MAY ALSO LIKE

Emertxe’s Online Training Programs in Embedded Systems & IoT

Emertxe’s Online Training Programs in Embedded Systems & IoT

The reason why Emertxe is the best choice for career advancement: eLearning and Online training programs are the way forward in the COVID-19 disrupted world. Riding along the digital revolution will ensure engineers are future-ready with skills to not only secure but...

Our Training Programs for Freshers

Our Training Programs for Freshers

Introduction: Emertxe is the leading training institute in Bangalore for Embedded Systems and IoT domains. It is a pioneer in training freshers since 2003 by providing excellent placement opportunities for freshers. Over the years 70000+ students have made their...

Q