C Enumerated Data Type

C Enumeration or Enumerated Data Type C enumeration is a

client characterized information type which comprises of a

rundown of names and each name relates to a necessary consistent. It builds the program

coherence as names are anything but difficult to peruse and keep up. Counted information

type can be characterized in C utilizing “enum” watchword. The syntax is given underneath.

Syntax

enum identifier_name {name1, name2, . . . . namen};

Example

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

In above example we have characterized an identified information type day. Presently

we need to pronounce factors of this information type. The factors can store any name present in

the rundown of names. The factors can be proclaimed in following manner.

enum day first_day, last_day;
first_day = mon;
last_day = sun;

Above we have announced two factors. The variable can likewise be proclaimed at the hour

of characterizing the specified information type. For doing this simply compose the variable name

after the end supports and before the semicolon.

As I have just disclosed to you that each name in the rundown compares to an indispensable worth.

As a matter, of course, the vital worth beginnings with 0. In above example “mon” will compare to 0, “tue” will relate

to 1, “marry” will compare to 2, etc. We can likewise appoint irregular vital qualities expressly. It tends to be done in the following manner.

enum day {mon=5, tue=7, wed=8, thu=10, fri=15, sat=25, sun=19}; //example 1
enum day {mon=2, tue, wed, thu, fri, sat, sun}; //example 2

In first example I have allotted irregular qualities to each name. In second example I

have relegated an incentive to just a first name for example mon=2. The rest names will be relegated values that expanded progressively by 1.

Keep in mind that the qualities must be the whole number just and two names can have the same worth.

Lets make one program to comprehend the idea of C counted information type.

#include<stdio.h>
 
void main()
{
 enum day{mon,tue,wed,thu,fri,sat,sun};
 enum day d1,d2;
 d1=tue;
 d2=thu;
 printf("%d",d1);
 printf("n%dn",d2+2);
}

Output:

1
5
Press any key to continue . . . _

Clarification

Above all else, I have proclaimed a counted information type day which contains rundown of names of days in seven days.

As I have not allowed qualities to these names expressly so the compiler will allocate them esteems beginning from 0.

Presently I have pronounced two factors d1 and d2 of this counted information type and doled out them names from the name list.

Presently I am printing the factors. In the first printf() articulation, the output is 1 in light of the

fact that d1 contains “tue” and it relates to whole number worth 1.

In second printf() articulation the variable d2 contains “thu” which compares to whole number

worth 3. As I have composed d2+2 so it will be equivalent to 5.

This is about C enumeration. On the off chance that you have any questions or discover

any misstep in above instructional exercise, at that point please notice it by remarking underneath.

Leave a Comment

error: Alert: Content is protected!!