What is enumerated data type in C language?


These are used by the programmers to create their own data types and define what values the variables of these datatypes can hold.

The keyword is enum.

Syntax

The syntax for enumerated data type is as follows −

enum tagname{
   identifier1, identifier2,…….,identifier n
};

Example

Given below is an example for enumerated data type −

enum week{
   mon, tue, wed, thu, fri, sat, sun
};

Here,

  • Identifier values are unsigned integers and start from 0.
  • Mon refers 0, tue refers 1 and so on.

Example

Following is the C program for enumerated data type −

 Live Demo

#include<stdio.h>
main ( ){
   enum week {mon, tue, wed, thu, fri, sat, sun};
   printf ("Monday = %d", mon);
   printf ("Thursday = %d", thu);
   printf ("Sunday = %d", sun);
}

Output

When the above program is executed, it produces the following result −

Monday = 0
Thursday =3
Sunday =6

Here, enum identifier can assigned initial value.

Example

Given below is the another C program for enumerated data type −

 Live Demo

#include<stdio.h>
main ( ){
   enum week {mon=1, tue, wed, thu, fri, sat, sun};
   printf ("Monday = %d", mon);
   printf ("Thursday = %d", thu);
   printf ("Sunday = %d", sun);
}

Output

When the above program is executed, it produces the following result −

Monday = 1
Thursday =4
Sunday =7

Updated on: 24-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements