17 - C Union and Enumerated Data Types

This chapter is going to introduce union and enumerated data types. In other words, this chapter will cover two topics, union and enumerated data types.

17.1 Enumerated data type

Enumerated data type is a user defined data type. It grabs integer vales from a list. Integer values are actually replaced by alphabetic or descriptive names. It increases ease of read of the program. These detailed names are known as enumerators or enumerator constants. Together they form a list which is apparently called enumerator list. Format of enumerated data type is as shown below:

   enum tag_of_name

   {

      member_1

      member_2

      ...

   };

 Figure Format of enumerated data type

 

Explanation: Here,

                 enum is the keyword

                 tag_of_name is the identifier which represents enumerated data type

                enum tag_of_name represents derived data type when combined together

                member_1, member_2,... represents constants of type integer. These integer constants  are represented by their detailed names. These are known as enumerators. 

Member_1, member_2 are not variables. They are constants. Compiler itself assigns value 0 to first member or member_1, value 1 to member_2 and subsequent members will be assigned the values in increasing order sequentially.

 

Let us consider an example of enumerated data type so that it can be better understood:

enum employee_depart

  {

    it, operations, management, accounts, stores

  };

Explanation: Here,

            enum is the keyword

            employee_depart is the name of tag or name of enumerated data type

            it, operations, management, accounts, stores are enumerators

 

Compiler will assign vale 1 to it, operations will be assigned value 2 and so on.

Since we know the syntax of defining enumerated data type next step is to declare variables for this data type. Syntax is as shown below:

            enum tag_of_name variables;

Let us consider the above example of enumerators and using that the variables will be declared as :

              enum employee_depart emp, em; 

Explanation: Here,

                        enum employee_depart is the derived data type

                        emp, em are the variables of enumerated data type 

We can explicitly assign values to these enumerators also. Let us consider an example:

enum months
{
  january, february, march, april, may, june, july, august, september, october, november, december
};
enum months month1, month2

Explanation: Here,

                        enum months is the derived data type

                        january, february, march,..., december will assigned values from 0, 1, 2, …, 11  respectively.

                        month1, month2 are the variables of enumerated data type

 

We can assign values to these members. Let us consider the example


enum months
{
  january=4, february, march, april=0, may, june, july
}month1;

Explanation: Here,

             january=4 will assign value 4 to january then february will be assigned value 5 followed                 by march with value 6. Now april is assigned value 0 then may will be assigned 1                            followed by june with value 2 and then july to value 3 respectively. 

Let us consider a program to illustrate the enumerated data type. 

/* Program to illustrate enumerated data type*/

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>

void main()
{

  enum employee_depart
  {
    it, operations, management, accounts, stores
  };

  struct employee_records
  {
   char name[10];
   int employee_id;
   float salary;
   enum employee_depart emp;
  }; 

  struct employee_records em;

  strcpy(em.name, "Debasif");
  em.employee_id=546502;
  em.salary=40000;
  em.emp=management;

  printf("Name of the employee=%s\n", em,name);
  printf("“Employee id=%d\n", em.employee_id);
  printf("Employee salary=%7.2f\n", em.salary);
  printf("Employee department=%d\n", em.,emp);
  getch();
}

Program looks similar in the editor as in the following snapshot:

Program to illustrate enumerated data type

After compiling programs looks similar as in following snapshot:

After compiling Program to illustrate enumerated data type

Output of program looks as shown in following snapshot:

Output of program to illustrate enumerated data type

17.2 Unions

Union is also known as derived data type. Union is also known as collection of variables under one name. These variables can be of like and alike data types. Each variable is called field or member which is similar to structures. But do not get confuse between structures and unions. Unions are functionally different from structures. Format of union is as follows:

       union tag_of_name

      {

        data_type_1 member1;

       data_type_2 member2;

       …                    ...

    };

   Figure Format of union

Explanation: Here,

                union is the keyword

                tag_of_name is the name of identifier

                union tag_of_name represents the derived data type when combined together

                data_type_1, data_type_2 are the data types of members

                member1, member2 are the fields or members of union

Let us consider an example of union

   union type
   {
     int i;
     double d;
     char c;
    } data;

 

Explanation: Here,

             union is the keyword

             type is the name of tag

             union type represents the derived data type

             d,i,c are the members of union of data type double, integer, char respectively

             data is a variable of type union 

By defining union memory is not allocated. Memory is allocated after declaring the variable. In the above example memory will be allocated after declaring data. 

We can access the members of union as we access the members in structures. Syntax is variable followed by dot (period) then name of member

 

variable.member 

Figure declaring variable for union 

Let us consider the above example, then to access i we will specify data.i. To access d we will specify data.d and c can be accessed by data.c respectively.

Let us consider an example to demonstrate union data type also.

 

/* Program to illustrate unions in C */

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
# include <string.h>

void main()
{

   typedef union
   {
    int i;
    double d;
    char c;
   }TYPE;

   TYPE data;
   data.i=50;
   data.d=45.68;
   data.c='A';

   printf("Value of i=%d\n", data.i);
   printf("Value of d=%5.2f\n", data.d);
   printf("Value of c=%c\n", data.c);
   getch();
}

Program looks similar in editor as in the following snapshot:

Program to illustrate unions in C

                                             Figure - Program to illustrate unions in C

Program looks similar as shown in the following snapshot after compiling the program

After compiling Program to illustrate unions in C

                                     Figure - After compiling Program to illustrate unions in C

Output of program is as shown in the following snapshot:

 

                           FIgure - Output of Program to illustrate unions in C

 

We are concluding this chapter here. Next chapter is dedicated to typedef. Thank you.

Like us on Facebook