Difference Between Structure and Union in C – Tabular Form

Difference Between Structure and Union in C – Tabular Form

Difference between Structure and Union: In C or C++, sometimes we need to store different types of data for the same person or object. In that case, structure or union is used according to the requirements. Both structure and union are user-defined data types with different functionalities. Both are used to hold data of any type.

Structure

A structure is the combination of different kinds of data member enumerated in one user-defined data type struct with different structure variables. It has the syntax as follows:

struct structure_name
{
    //member definition;
} structure_variable;

While defining a structure, we must use the keyword struct. Here, structure_variable is the object of the struct type. In structure, we can create as many objects as we want. For accessing the data member of the structure, dot(.) operator is used along with the structure_variable. For example:

       #include <stdio.h>
       #include <conio.h>
       struct data
       {
            int a;
            float b;
            char c;
       } d1, d2;
       void main()
       {
            d1.a=5;
            d1.b=1.24;
            d1.c='r';
            printf("\n a= %d",d1.a);
            printf("\n b= %f",d1.b);
            printf("\n c= %c",d1.c);
            printf("\n size of d1: %d",sizeof(d1));
            printf("\n size of d2: %d",sizeof(d2));
            getch();
       }
It will give the output as:
    a= 5
    b= 1.24
    c= r
    size of d1= 7
    size of d2= 7

In the above example, both the objects d1 and d2 occupies 7 bytes. Hence, total memory space required to store a structure is equal to the sum of size of memory of all the members in the structure. Hence in the above example, int (2 bytes) + float (4 bytes) + char (1 byte) = 7 bytes of memory required for both the structure variable d1 and d2. 

Union

Union is the combination of different kinds of data member stored in the same memory space. The keyword union is used to define a union data types. It has the syntax as follows:

union union_name
{
   //member definition;
} union_variable;

Here, union_variable is the object of the data type union. Like struct, we can create as many objects of a union as we want. Like struct, in the union also dot(.) operator is used along with the union_variable accessing the data member of the union. For example:

      #include <stdio.h>
      #include <conio.h>
      union data
      {
         int a;
         float b;
         char c;
      } d1,d2;
      void main()
      {
           clrscr();
           printf("accessing all the data at the same time:");
           d1.a=5;
           d1.c='p';
           d1.b=1.24;
           printf("\n %d",d1.a);
           printf("\n %f",d1.b);
           printf("\n %c",d1.c);
           printf("\n accessing the data one by one:");
           d2.a=9;
           printf("\n %d",d2.a);
           d2.b=78.29;
           printf("\n %f",d2.b);
           d2.c='x';
           printf("\n %c",d2.c);
           printf("\n size of d1: %d",sizeof(d1));
           printf("\n size of d2: %d",sizeof(d2));
           getch();
      }
It will give the output as:
    accessing all the data at the same time:
    a= -18009
    b= 1.24
    c= R
    accessing the data one by one:
    a= 9
    b= 78.29
    c= x
    size of d1: 4
    size of d2: 4 

In the above example, both the object d1 and d2 occupies 4 bytes only. Since, in union total memory space allocated is equal to the memory size required by the largest data member. Here, float requires highest memory space of 4 bytes.  Again, we can see some faulty outputs of the program. This is because of the sharing of memory space by all the data members of the union. Only that value of the data member can be accesses at a time whose value is recently stored in the allocated memory location.

Similarities between structure and union:

 

  • Both structure and union have the same way of declaration. In both structure and union, the keyword of each used and data members are defined inside the block and member variables are defined at the end of the block followed by semicolon(;).
  • Both structure and union has the same way of accessing data member using member variable followed by dot(.) operator.
  • Both structure and union supports assignment(=) and sizeof() operator. In case of assignment operator, both the L.H.S. and R.H.S. structure/union must have the same number of data members.

Difference between structure and union:

 structureunion
keywordTo define a structure, struct keyword is used.To define a union, the union keyword is used.
initializationAll members can be initialized at once.One member can be initialized at once.
syntaxstruct structure_name {
//member definition;
} structure_variable;
union union_name {
//member definition;
} union_variable;
sizeTotal memory size required is equal to the sum of the size of memory of all the members in the structure.Total memory size required is equal to the memory size required by the largest data member in the union.
memory allocationEach member has a unique memory space.All the members have the same memory space.
accessingAll the members can be accessed at the same time.One member whose value recently stored in the memory location can be accessed at a time.
value alteringAltering a value will not affect other members.Altering a value will also alter the value of other members
usesUsed when programmers need to store distinct values for all the members in a distinct memory location.Used when type conversions are needed.

This article is contributed by Upashana Baruah

Liked this article? Then please like our Facebook Page & stay connected with us.

Also Read: Difference Between C and C++

Feel Free to Share this:

Debarshi Das

Debarshi Das is a passionate blogger & full-stack JavaScript developer from Guwahati, Assam. He has a deep interest in robotics too. He holds a BSc degree in Information Technology & currently pursuing Masters of Computer Application (MCA) from a premier govt. engineering college. He is also certified as a chip-level computer hardware expert from an ISO certified institute.

Leave a Reply

Close Menu