enum in C

enum in C: An enum in C  (or) enumeration is a user-defined data type in C. It is mainly used for assigning names to integral constants, the names make a program easy to read and maintain. And the size of the enum data type size is 2 byte. Using enum we can create a sequence of integer constants.

Syntax: enum tagname {value1, value2, value3,…};

enum in C with Examples

  • The values assigned to the enum names must be some integral constant i.e. the value must be in the range from minimum possible integer value to maximum possible integer value.
  • There is no specific use of enum because we just use it to make our code neat and more readable.
  • It is allowed to have multiple names with the same value.
  • It has the advantage to follow the standard scope rules in C.
  • And it also requires the correct naming of the tag and constants of the scope.

Example

#include<stdio.h>
#include<conio.h>
enum ABC {x,y,z};
void main(){
int a;
clrscr();
a=x+y+z;
printf("Sum: %d",a);
getch();}

Output: sum:3

Example 2

#include<stdio.h>
enum week{sunday,monday,tuesday,wednesday,thursday,friday,saturday };
int main(){
enum week today;
today = wednesday;
printf("Day %d",today+1);
return 0;}

Output: Day 4

  Example 3
#include<stdio.h>
#include<conio.h>
main(){
int roll1,roll2;
enum standard {FIRST,SECOND,THIRD,FOURTH};
enum standard s1,s2;
clrscr();
printf("\n Enter the roll numbers for two students");
scanf("%d%d",&roll1,&roll2);
s1=FIRST;s2=FOURTH;/*assigning the standards*/
printf("\nThe Roll Number %d is in %d st Standard",roll1,s1+1);
printf("\nThe Roll Number %d is in %d th Standard",roll1,s2+1);
getch();}