• C Programming Video Tutorials

Enumeration (or enum) in C



Enumeration (or enum) in C

C enumeration (enum) is an enumerated data type that consists of a group of integral constants. Enums are useful when you want to assign user-defined names to integral constants. The enum keyword is used to define enums.

Defining and Declaring an Enum Type

To declare and define an enumeration (enum) data type, use the enum keyword followed by the enum name and assign all values inside the curly braces.

Syntax

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

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

Enum Variable Declaration

After declaring an enumeration type, you can also declare its variable to access enumeration members (constants). To declare an enum variable, write the enum keyword followed by the enaum name (enum_name) and then the variable name (var).

Syntax

Below is the syntax to declare a variable of enum type −

enum enum_name var;

Example

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.

Change Enum Constants Values

When we declare an enum type, the first constant of the enum is initialized with 0 by default, the second constant is initialized with 1, and so on. We can also assign any integer value to any constant of enum.

Example

In the following example, we are declaring an enum type and assigning different values to the enum constants.

#include <stdio.h>

enum status_codes { OKAY = 1, CANCEL = 0, ALERT = 2 };

int main() {
   // Printing values
   printf("OKAY = %d\n", OKAY);
   printf("CANCEL = %d\n", CANCEL);
   printf("ALERT = %d\n", ALERT);

   return 0;
}

Output

It will produce the following output −

OKAY = 1
CANCEL = 0
ALERT = 2

Enum in Switch Statements

C language switch case statement works with integral values. We can use enum type to define constants with (or, without) integral type values to use them in switch case statements.

Example

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

#include <stdio.h>

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

int main() {
   // Enum variable declaration
   enum colors color = YELLOW;
   // switch statement using enum
   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

Examples of Enumeration (enum) Type

Practice the following examples to understand the concept of enumeration (or, enum) type in C programming language.

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

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