C Programming Tutorial
About Lesson

C Enumeration

In this tutorial, we will learn about enumeration in C programming with the help of examples.


Enumeration

In C programming, an enumeration is a special class that represents a group of constants.

The enum keyword is used to create enumeration classes.

Note : enum is a user defined data type where we specify a set of constants and the constants declared inside enum are seperated by commas.

Syntax :

enum flag {
  const1,const2, ..., constN
};

By default, const1 or the first item in the list is assigned with the value 0, const2 or the second item in the list is assigned with the value is 1 and so on.
We can change default values of enum elements according to our requirement during declaration.

// Changing default values of enum constants
enum card_suit {
  diamond = 1,heart = 12,club = 10,spades = 4
};
Note : The compiler will automatically assign the default values to the enum names starting from 0 when we do not assign any value to them.
Note : The assigned value to the enum names must be integeral constant.

Enumerated Type Declaration

We have to declare enum type or enum variable to access enum types.
Here, how we can create variable of enum types :

enum day {
  sunday , monday, tuesday, wednesday, thursday, friday, saturday
};
enum boolean today; // declaring an enum variable "today"

Here, a variable today of the type enum day is created.

Another way of creating variable of enum type.

enum day {
  sunday , monday, tuesday, wednesday, thursday, friday, saturday
} today; // declaring an enum variable "today"

Here, the value of sunday is 0, value of monday is 1, value of tuesday is 2, value of wednesday is 3, value of thursday is 4, value of friday is 5,and the value of saturday is equal to 6.


Example: Enumeration Type

#include <stdio.h>

enum day {
  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

int main() {
  enum day today; // declaring an enum variable "today"
  today = Monday;
  printf("Day %d",today+3);
  return 0;
}

Output

Day 4

Why enums are used?

We use enum when we want our variable to have only a set of values or when require values that are not going to change (e.g. days of the week, keys of keyboard, list of names, deck of cards, colors of rainbow etc.)

It is commonly used in switch-case statements.

#include <stdio.h>
enum day {
  Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
};

int main() {
  enum day today = 4;
  switch(today) {
  case Sunday:
    printf("The day today is Sunday");
    break;
  case Monday:
    printf("The day today is Monday");
    break;
  case Tuesday:
    printf("The day today is Tuesday");
    break;
  case Wednesday:
    printf("The day today is Wednesday");
    break;
  case Thursday:
    printf("The day today is Thursday");
    break;
  case Friday:
    printf("The day today is Friday");
    break;
  case Saturday:
    printf("The day today is Saturday");
    break;
  default :
    printf("Something went wrong, Try again!");
  }
  return 0;
}

Output

The day today is Thursday

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!