Enumeration

After reading this enumeration topic, you will understand its theory and examples also you will able to implement it in C programming.

Enumeration is a user-defined data type and similar to the structure as well as the union but all the enumeration members are constants.

General Form (Syntax):

The syntax of enumeration  in C is given below,

enum enumeration_name{member1,member2,….};

Here, the enum keyword at the beginning used before writing enumeration members as member1,member2,…. in the curly braces (i.e. {}).

The example below will show the demo of enumeration C programming.

Example:

Program (1): To demonstrate Enumeration in C.

#include<stdio.h>
#include<conio.h>
void main(void)
{
enum play{cricket,football,hockey};
int choice;
printf("Enter sport to be play :- 0 for Cricket,1 for Football,2 for Hockey :- ");
scanf("%d",&choice);
switch(choice)
{
case cricket:printf("You have select Cricket");
break;
case football:printf("You have select football");
break;
case hockey:printf("You have select hockey");
break;
}
getch();
}

Output (1)

Enter sport to be play :- 0 for Cricket,1 for Football,2 for Hockey :- 1
You have select football

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

Your email address will not be published. Required fields are marked *