Register Login

Difference between Structure and Union in C Language

24 Dec 2018 9:23 am || 0

C provides five different ways of creating custom data. These are bit-field’, ‘union,’ ‘structure,’ ‘‘enumeration’ and ‘typedef.’ This article aims to throw more light on the main differences between structure and union in C. Read on to know more about structure vs. union.

Structure vs. Union

Union and structure in C are container data types designed to hold any data. An important point of distinction between structures and unions in C is that structure possesses a separate memory location that is allocated to each member. In contrast, the members forming a union possess the same memory location.

Now that you are aware of the main structure and union difference, here’s a table to facilitate the cause further:

Point of Difference Union Structure
Keyword ‘union’ is used to define a union ‘struct’ is used to describe a structure
Shared memory space Members share memory space Members share no memory space
Retrieval and Accessibility of members Only a single member can be retrieved and accessed at a time Any member is capable of being retrieved any time
Initialization Only the first member is capable of being initialized Several members can be initialized at any given time
Size Size of a union is equal to that of the largest member The size of a structure is similar to the total of the dimensions of all members
Alteration in value Change in value of one will affect the value of other members Alteration in value of any member fails to impact the value of others
Values Union stores the same value as attributed to all members Structure stores different values for its members.
Way of Viewing Offers multiple ways of viewing the same memory location A single way is being provided for viewing each memory location
Anonymous declaration An anonymous union can be declared An unknown structure cannot be declared

What is Structure in C Language?

Structure in C pertains to a user-defined container data type that helps users combine data types comprising of different kinds to form a single type in any given block of memory. It is capable of containing both complex and simple data types that are related to each other, which would fail to make sense otherwise. All members present in the structure are provided with their memory locations that can be retrieved and accessed readily and at any time.

C programmers opt for a structure when large quantities of data require grouping as in a member directory used for the storage of name, address, numbers, etc. of different members belonging to a club or association. The information about each member is usually depicted in ascending order with each memory location starting at different offset values. Any alteration in the value of a specific member will not have any impact on the values provided to other members in a structure.

The ‘struct’ keyword is utilized for defining a structure of different data types falling under a single name. The ‘struct’ keyword informs the compiler that a specific structure is declared.

It is possible to access the members of a structure via two types of operators:

a. Structure pointer operator

b. Member operator

Code Example of Structure in C

#include <stdio.h>
#include <string.h>
 
struct Books_information {
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
   float book_price;
};
 
int main( ) {

   struct Books_information Book;        /* Declare Book of type Book_information */
 
   /* Book Information */
   strcpy( Book.title, "C Programming");
   strcpy( Book.author, "Stechies");
   strcpy( Book.subject, "C Programming Tutorial");
   Book.book_id = 181193;
   Book.book_price=200.78;
 
   /* Display Information */
   printf( "Book Id : %dn", Book.book_id);
   printf( "Book Title : %sn", Book.title);
   printf( "Book Author : %sn", Book.author);
   printf( "Book Subject : %sn", Book.subject);
   printf( "Book Price : %fn", Book.book_price);
   return 0;
}

Output

Book Id : 181193
Book Title : C Programming
Book Author : Stechies
Book Subject : C Programming Tutorial
Book Price : 200.779999

What is a Union in C?

Unions in C are in the form of a particular data type that allows for the storage of different kinds of data types in a single memory location. Though it is possible to define a union containing several members, only a singular member can be provided with a value at any given point of time. In general, unions in C work to use any given memory location for different purposes. A union is considered to be quite similar to a structure in C. Union in C is defined by using the keyword ‘union’. In this case, the members overlay the memory allocated to each other. Usually, unions are large enough to fit in all its members.

When the union becomes associated with a variable, the compiler considers the size of the most massive available memory and allocates the same to it. In other words, the size of the largest data member defines the size of a union and is equal to it. As the address of all members in a union is the same, it effectively means that each member would begin at the same offset value. Any alteration in the value of a specific member will impact the values provided to other members in a union. In general, C programmers use a union for storing values belonging to several data types.

The keyword ’ union defines union’, and its declaration is quite similar to that of a structure. Here, the variable is capable of storing values of different data types – be it an integer, string, or float.

Code Example of Union in C language:

#include <stdio.h>
#include <string.h>
 
union Data {
   int id;
   float price;
   char title[20];
};
 
int main( ) {

   union Data data;       

   data.id = 10;
   printf( "Book.id : %dn", data.id);
  
   data.price = 220.5;
   printf( "Book.price : %fn", data.price);
  
   strcpy( data.title, "C Programming");
   printf( "Book.title : %sn", data.title);

   return 0;
}

Output

Book.id : 10
Book.price : 220.500000
Book.title : C Programming

Key Difference between Union and Structure

  1. Keyword: A structure is defined by using the keyword ‘struct’ while ‘union’ is the keyword used for determining a union.
  2. Memory Allocation: Within a specific structure, separate memory space is allocated for each member. Each member occupies a different memory location and does not share any memory. On the other hand, a union delegates the same memory space for all members, and the allocation of shared memory location becomes a possibility.
  3. Member Access: Only one member in a union is accessible at any given point of time, even though the union stores a singular value for all its members. Conversely, as multiple values are capable of being stored in a structure, a member can be retrieved and accessed at any given point of time.
  4. Size: The union’s size is equal to the size of its largest member, while the size of a structure is equivalent to the sum of the dimensions of all members, or more.
  5. Initialization: In the case of a union, the first member is the only member that is initialized with the value provided to its data type. Conversely, several members are capable of being initialized in a single go in case of a structure.
  6. Value: A union stores the same value as provided to all its members; the change in the value of a single member impacts the value of all others. On the other hand, a structure stores different values provided to all its members; any change in the value of a member will not impact the value of others.

Similarities between Structure and Union in C

  • Both structures and unions are user-defined data types that are useful for storing data of various kinds in the form of a single unit.
  • The members of a union and structure can be in the form of objects of any data type, including arrays or other unions and structures. It is also possible for a member to comprise of a bit field.
  • Other structures and unions support assignment = and size of operators. The structures/ unions in the assignment should comprise of the same member types and members.
  • A union or structure can be returned by ‘value by functions’ and passed by ‘value to functions.’ Here, the argument has to be of the same type as that provided to the function parameter. A union or structure is to be passed by value, like scalar variables, and as a given corresponding parameter.
  • The ‘.’ operator is put to use for accessing members in both cases.

Conclusion

Both structure and union are container datatypes that contain members belonging to different types. The use of structure and union in c programming is dependent on the purpose of their application. Programmers use structures for the sake of storing distinct value for its members in a single memory location. On the other hand, a union in the c program comes in handy when type conversion is required.

We hope that you have gained a fair understanding of structure vs. union in c in this article. It will help to go through the application in structures and unions in c examples to improve upon your understanding further.