Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Enumerated Data Type in C

The enumerated data type is also known as the enum in the C language. Now, enum is not a basic, but a user-defined form of data type that consists of various integer values, and it functions to provide these values with meaningful names. Using the enum data type in C language makes any program much easier to maintain as well as understand. We define enums using the keyword “enum”.

In this article, we will take a closer look at the Enumerated Data Types in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Definition of Enumerated Data Type in C

Here is how we can define an enum in C language:

enum flag{integer_constant1, integer_constant2,…..integter_constantN};

As you can see in the declaration mentioned above, one would define an enum to be named as a flag that contains about ‘N’ numbers of integer constants. Here, the default value of any integer_constant1 would be equal to 0. Then the value of integer_constant2 would be 1, for integer_constant3 it would be 2, and so on. However, it is just the default value and we can change it when declaring an enum.

Let us take a look at an example:

enum week{Mon, Tue, Wed, Thurs, Fri, Sat, Sun};

Here, the default value assigned to Mon is 0, Tue is 1, Wed is 2, Thurs is 3, Fri is 4, Sat is 5, and Sun is 6. In case we want to change all of their default values, then here is what we should do:

enum week{

Mon=13,

Tue=27,

Wed=45,

Thurs=86,

Fri=921,

Sat=364,

Sun=578,

};

Declaration of Enumerated Data Type in C

As we all know, we must declare any variable in the C language, which is basically of a pre-defined type, like char, float, int, etc. In a similar manner, we can also declare the variables present in the user-defined data types like the enum data type. Now, here is how one can declare any variable present in an enum data type.

Let us assume that we are creating an enum of true and false status. It will appear as follows:

enum status{true,false};

Now, here is how we create a variable for the status enum type:

enum status x; // creation of a variable that belongs to the status type.

In the case of the statement mentioned above, we have performed the declaration of the variable ‘x’ of the type status. Thus, if we want to create variables, then we can write the two statements mentioned above as:

enum status{true,false} x;

Here, the default value for true would be equal to 1, while for false, it will be equal to 0.

Why use an enum in a program?

One can utilise the enums in a program when they want the variables in the program to have just the set of values. For instance, let us consider the creation of a direction variable. Now, there are a total of four directions (N, S, W, E). Thus, a total of four values are possible with the direction variable. But here, the variable would be able to hold just a single value at any given time. Now, if a user provides any different value to the variable here, then we would get a compilation error.

We can also use the enum in the case of switch case statements. In this statement, we pass all the enum variables within the switch parenthesis. This step would basically ensure that we have to define the value of the case block in an enum.

Now, we will see how one can make use of an enum within a switch case statement. Let us take a look:

#include <stdio.h>

enum vegetables{broccoli=1, spinach, carrot, eggplant, celery, pumpkin, shallot};

int main()

{

enum vegetables v;

v=broccoli;

switch(v)

{

case broccoli:

printf(“I will eat broccoli”);

break;

case spinach:

printf(“I will eat spinach”);

break;

case carrot:

printf(“I will eat carrot”);

break;

case eggplant:

printf(“I will eat eggplant”);

break;

case celery:

printf(“I will eat celery”);

break;

case pumpkin:

printf(“I will eat pumpkin”);

break;

case shallot:

printf(“I will eat shallot”);

break;

}

return 0;

}

The output generated here would be:

I will eat broccoli

Important points to remember about enum

  • The names of enums available in any enum type are capable of having the very same value. Take a look at the following example to understand this better.

#include <stdio.h>

int main(void) {

enum cosmetics{lipstick= 1, blush=0, mascara=1};

printf(“The given value of lipstick is %d”, lipstick);

printf(“\nThe given value of mascara is %d”, mascara);

return 0;

}

The output generated here would be:

The given value of lipstick is 1

The given value of mascara is 1

  • In case we fail to provide any values to these enum names, the compiler would then assign these enums with a default value, starting from 0 (for the first one).
  • A programmer can provide any values to the enum names in any order they like.
  • All the unassigned enum names would be assigned with a default value automatically, which would be equal to the value of the previous enum plus one.
  • Any value that we assign to these enum names must be in the form of integral constants. It means that it must not be of any other type, like float, string, etc.
  • All of the enum names must have a very unique scope of their own. In simpler words, whenever we define two separate enums with the very same scope, then it is necessary that these two enums have separate enum names. If it doesn’t happen, then the compiler would throw a compile-time error.
  • We can also define any enumerated data type in an enumeration without its name.

Practice Problems on Enumerated Data Types in C

1. What would be the output obtained out of the program mentioned below:

#include <stdio.h>

int main(void) {

enum food{dhokla= 1, thepla=0, khakra=1};

printf(“The value of dhokla is %d”, dhokla);

printf(“\nThe value of khakra is %d”, khakra);

return 0;

}

A. The value of dhokla is 0

The value of thepla is 1

B. The value of dhokla is 1

The value of khakra is 1

C. The value of dhokla is 0

The value of thepla is 0

D. The value of dhokla is 1

The value of khakra is 0

Answer – B. The value of dhokla is 1

The value of khakra is 1

2. What would be the output obtained out of the program mentioned below?

#include <stdio.h>

enum cellphone{Samsung=1, Motorola, Apple, RealMe, Oppo, Vivo, Nokia};

int main()

{

enum cellphone c;

c=RealMe;

printf(“The value of c is %d”,c);

return 0;

}

A. The value of c is 1

B. The value of c is 4

C. The value of c is 7

D. The value of c is 3

Answer – B. The value of c is 4


Frequently Asked Questions

Q1

What is the use of enum in a program if we already use the primary data types?

One can utilise enums in a program when they want the variables in the program to have just the set of values. For instance, let us consider the creation of a direction variable. Now, there are a total of four directions (N, S, W, E). Thus, a total of four values are possible with the direction variable. But here, the variable would be able to hold just a single value at any given time. Now, if a user provides any different value to the variable here, then we would get a compilation error.
We can also use enum in the case of the switch case statements. In this statement, we pass all the enum variables within the switch parenthesis. This step would basically ensure that we have to define the value of the case block in an enum.

Q2

Can two different enum types have the same values in an enum?

The names of enums available in any enum type are capable of having the very same value. Take a look at the following example to understand this better.
#include
int main(void) {
enum cosmetics{lipstick= 1, blush=0, mascara=1};
printf(“The given value of lipstick is %d”, lipstick);
printf(“\nThe given value of mascara is %d”, mascara);
return 0;
}
The output generated here would be:
The given value of lipstick is 1
The given value of mascara is 1

Keep learning and stay tuned to get the latest updates on the GATE Exam along with Eligibility Criteria, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*