Structure vs Union in C: Difference and Comparison

In C programming language, there are predefined data types and user-defined data types. Examples of user-defined data types are structures and unions in C.

In Structure and Union, users can define different data types and member functions to access all the variables. Although they both follow the same syntax, there is a vast difference between them.

Key Takeaways

  1. Structures in C allow for grouping variables of different data types under a single name while maintaining individual memory allocations for each member, allowing for simultaneous storage of all members’ values.
  2. Unions in C also allow for grouping variables of different data types under a single name but share a single memory allocation for all members, meaning only one member’s value can be stored at a time.
  3. The key difference between structures and unions lies in their memory allocation and value storage, with structures using separate memory for each member and unions sharing memory among all members.

Structure vs Union in C

In structure, every input data member has a specific memory location. As a result, it is capable of storing numerous values for each member. While in the union, a single shared memory is allocated for all the input data members. As a result, it saves a single value at a time for all the members.

Structure vs Union in C

For example

                        Struct Work

                                    {

                                                Int money;

                                                Char name;

                                    }s1;

In this, the size of memory for the struct allocated would be 2+1 =3 bytes since the size of the integer is 1 byte, and the size of the char is 2 bytes. Hence the size will be 3 bytes.

`                       union Work

                                    {

                                                Int money;

                                                Char name;

                                    }s1;

In the union, the size of memory allocated will be 2 bytes since the maximum size of data type in the union is 2 bytes, i.e. character data type.

Also Read:  Perl vs Python: Difference and Comparison

 

Comparison Table

Parameter for ComparisonUnionStructure
Keyword definitionIt uses the keyword “union.”Uses the Keyword “struct.”
Memory sizeSize is equal to the element of the largest size.Size is equal to the sum of all the elements in the structure
Shared MemoryAll the element’s memory is shared with different elementsStructure elements do not have shared memory.
Element AccessOnly a single element is accessed at a single timeAny number of elements can be accessed at any point in time
ExampleUnion example
{
Data Types and members
}obj;
struct example
{
Data Types and members
}obj;
Pin This Now to Remember It Later
Pin This

 

What is Structure in C?

The structure is a user-defined data group that collects various data types. The following example will be pretty helpful. Suppose a programmer needs to store data like a student, name, class, address and many more.

Well, there are two ways to approach this problem, one is to create different data types, and another one is to create a structure.

In this case, the structure would be advantageous since, if you create variables separately, you need to create many variables for every student, and it would be really havoc. Instead, a structure can be used again and again.

A Structure can be created by using a keyword, struct.

For example

Struct structure_name{

                        Data type  datatype_name;

                        Data type  datatype_name;

                        Data type  datatype_name;

                        Data type  datatype_name;

                        …

            };

If you want to access any of the data members, you need to create an object, such as

Struct structure_name object_name;

With the syntax  object_name.datatype_name, you will be able to access the data type in the structure.

 

What is Union in C?

Similar to structures, the union is another user-defined data type in the C programming language. A user-defined datatype is made whenever a programmer defines a union, but there is no memory location.

Also Read:  Bandwidth vs Throughput: Difference and Comparison

If you want to allocate memory, you need to create variables in the union. Union has a property that does not allow any programmer to access the data types more than once.

Hence, in the union, a programmer can access one data member at a time. Let us take a small example.

            Union union_name

            {

                                    Data_type  Datatype_name;

                                    Data_type  Datatype_name;

                                    Data_type  Datatype_name;

                                    Data_type  Datatype_name;

            };

In the above example, the basic structure of a union is displayed. It is the way a programmer creates the union. If you want to access any data type in the union, you need to use it. An operator is similar to the structure.

Union union_name obj name; With the above statement, you can create the object that will help you to access any data member in the union using the dot operator.


Main Differences Between Structure and Union in C

  1. A structure is a user-defined data type that stores data types of different kinds. Also, it is used to represent a collection of data types’ values. If a programmer needs to define a union, then a programmer needs to use the keyword struct. At the same time, a union needs a union keyword for definition.
  2. In a structure, all the data types are stored in different locations, and you can access multiple data members. Whereas in a union, all data members share the exact memory location, and only a single data member can be accessed at a time.
  3. In structure, for initializing any member, several members can be initialized at a single time, but only the first member can be initialized in the case of a union.
  4. The structure’s total size depends on the sum of the data members’ size, whereas in the case of a union, the size is equal to the data member whose size is the largest among all.
  5. In structure, every data type has a different memory allocation, whereas, in the union, a single memory is allocated for the largest data member.
Difference Between X and Y 42
References
  1. Structures and Union – C Tutorial | Intellipaat.com
  2. Unions in C Programming: Definition & Example
One request?

I’ve put so much effort writing this blog post to provide value to you. It’ll be very helpful for me, if you consider sharing it on social media or with your friends/family. SHARING IS ♥️

Want to save this article for later? Click the heart in the bottom right corner to save to your own articles box!

About Author

Chara Yadav holds MBA in Finance. Her goal is to simplify finance-related topics. She has worked in finance for about 25 years. She has held multiple finance and banking classes for business schools and communities. Read more at her bio page.