• C Programming Video Tutorials

Enumeration (or enum) in C



The "enum" keyword in C is used to define an enumerated data type. An enumeration assigns user-defined identifiers to integral constants. Using enumerated data types makes the programs easier to read and maintain.

Defining and Declaring an Enum Type

This is the syntax you would use to define an enum type −

enum enum_name{const1, const2, ... };

You can now declare a variable of this user-defined type as −

enum_name var;

Let us define an enum type with the name myenum

enum myenum {val1, val2, val3, val4};

Identifier values are unsigned integers and they start from "0". val1 refers 0, val2 refers 1, and so on.

A variable of myenum type is declared as follows −

enum myenum var;

The possible constant values of myenum type are enumerated inside the curly brackets.

Example 1: Enum Constants Get Incrementing Integer Values

C assigns incrementing integer values to each constant, starting with "0". When we assign val2 to the above variable,

var = val2;

The integer value assigned to val2 is 1. Take a look at the following example −

#include <stdio.h>

enum myenum {val1, val2, val3, val4};

int main(){

   enum myenum var;
   var = val2; 
   printf("var = %d", var);
   
   return 0;
}

Output

It will produce the following output −

var = 1

Example 2: Enumerating the Weekdays

Let us declare an enum type weekdays to enumerate the names of the days and assign its value to the enum type variable −

#include <stdio.h>

int main(){

   enum weekdays {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
   
   printf ("Monday = %d\n", Mon);
   printf ("Thursday = %d\n", Thu);
   printf ("Sunday = %d\n", Sun);
}

Output

It will produce the following output −

Monday = 1
Thursday = 4
Sunday = 0

Example 3: Declaring a Variable of Enum Type

A typical application of enumerated data types is to assign weekdays to the integer values. In this code, we have declared a variable of weekdays enum type −

#include <stdio.h>

enum weekdays {Sun, Mon, Tue, Wed, Thu, Fri, Sat};

int main(){

   enum weekdays day;
   day = Wed;
   printf("Day number of Wed is = %d", day);

   return 0;
}

Output

When you run this code, it will produce the following output −

Day number of Wed is = 3

Example 4: Enum Values By Default Start from "0"

In this code, we define an enum type for names of the month in a calendar year. By default, C assigns the values starting from "0". For example, in the following C program, Jan gets the value "0", Feb gets "1", and so on.

#include <stdio.h>

enum months{Jan, Feb, Mar, Apr, May, Jun, Jul, 
   Aug, Sep, Oct, Nov, Dec};

int main(){

   for (int i = Jan; i <= Dec; i++)      
      printf("Month No: %d\n", i);

   return 0;
}

Output

Run the code and check its output −

Month No: 0
Month No: 1
Month No: 2
Month No: 3
Month No: 4
Month No: 5
Month No: 6
Month No: 7
Month No: 8
Month No: 9
Month No: 10
Month No: 11

Example 5: Starting an Enum from 1

To start enumeration from 1, assign 1 explicitly to the first value, the compiler will assign incrementing numbers to the subsequent values.

#include <stdio.h>

enum months{Jan=1, Feb, Mar, Apr, May, Jun, Jul, 
   Aug, Sep, Oct, Nov, Dec};

int main(){

   for (int i = Jan; i <= Dec; i++)      
      printf("Month No: %d\n", i);

   return 0;
}

Output

Run the code and check its output −

Month No: 1
Month No: 2
Month No: 3
Month No: 4
Month No: 5
Month No: 6
Month No: 7
Month No: 8
Month No: 9
Month No: 10
Month No: 11
Month No: 12

Example 6: Enumerating HTTP Status Codes

It is not necessary that the constants in the enum type should have incremental integer values. You can assign each of the constants a different and unique value, which may be in any sequence.

In the following code, the enum type enumerates HTTP status codes.

#include <stdio.h>
 
enum status {
   OK = 200,
   BadRequest = 400,
   Unauthorized = 401,
   Forbidden = 403,
   NotFound = 404,
   InternalServerError = 500,
};

int main(){

   enum status code =  InternalServerError;
   if (code == 500)
   printf("Internal Server Error has been encountered");

   return 0;
}

Output

Run the code and check its output −

Internal Server Error has been encountered

Example 7: Assigning a Fixed Number to Enum Constants

You can assign a fixed integer to some of the constants, leaving the compiler to assign values to others. All unassigned names get a value that is "value of previous name plus one".

#include <stdio.h>
 
enum myenum {a, b = 5, c, d, e = 10, f};

int main(){

   printf("a: %d\n", a);
   printf("b: %d\n", b);
   printf("c: %d\n", c);
   printf("d: %d\n", d);
   printf("e: %d\n", e);
   printf("f: %d\n", f);

   return 0;
}

Output

Run the code and check its output −

a: 0
b: 5
c: 6
d: 7
e: 10
f: 11

Example 8: Enumerating the Colors of Rainbow

In the following example, the colors in the rainbow are enumerated.

#include <stdio.h>

enum colors {VIOLET, INDIGO, BLUE, GREEN, YELLOW, ORANGE, RED};

int main(){

   enum colors color = YELLOW;
   switch (color){
      case BLUE: printf("Blue color"); break;
      case GREEN: printf("Green color"); break;
      case RED: printf("Red color"); break;
      default: printf("Color other than RGB");
   }

   return 0;
}

Output

Run the code and check its output −

Color other than RGB

Applications of Enums

We can use enums in C in different cases, some of which are listed below −

  • To store constant values, for example, weekdays, months, directions, colors, etc.
  • Enums are used for using flags, status codes, etc.
  • Enums can be used while using switch-case statements in C.
Advertisements