Enumerated data types in C Language

Enumerated data types in C Language
Enumerated data types in C Language

The enumerated data type gives us an opportunity to invent our own data type and define what values the variable of this data type can take. This can help in making the program listings more readable, which can be an advantage when a program gets complicated when more than one programmer would be working on it. Using enumerated data type can also help us reduce programming errors.

As an example, one could invent a data type called mar_status which can have four possible values-single, married, divorced or widowed. The enum declaration of mar_status and definition of variable of type mar_status is given below.


enum mar_status
{
	single, married, divorced, widowed
}enum mar status personi, person2;

Example:-


#include <stdio.h>
#include <string.h>
int main()
{
	enum department
	{
	assembly, manufacturing, accounts, stores
	};
	struct employee
	{
	char name[ 30]; int age; enum department dept;

	};
	struct employee e; 
	strcpy(e.name, "Lothar Mattheus" );
	e.age = 28;
	e.dept = manufacturing;
	printf ("Name = %s\n", e.name);
	printf("Age = %d\n", e.age);
	printf ("Department = %d\n", e.dept);
	if (e.dept == accounts)
		printf("%s is an accounant\n", e.name);
	else
		printf ("%s is not an accounant\n", e.name);
	return 0;
}

Output:

Name = Afaq
Age = 30
Department = 1
Afaq is not an accounant
c language tutorial learn c language study c language