Difference Between Structure and Union in C (With Table)

Structure vs Union in C

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 huge difference between them.

In Structure, the total memory size equals to the sum of the sizes of all the data types in structure. Whereas in Union the total memory space size is equal to the size of the largest data type in the union. It is one of the most important differences between structure and union.

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 union is 2 bytes i.e. character data type.


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

 

What is Structure in C?

Structure is a user-defined data group and is the collection of various data types. The following example will be quite useful. Let us suppose that a programmer needs to store some data like 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.

Here in this case structure would be advantageous since, if you create variables separately then 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 structure.

Structure in C 1
Structure in C
 

What is Union in C?

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

RECOMMENDED  Difference Between ProtonVPN and ExpressVPN (With Table)

Well, if you want to allocate memory then one needs to create variables in the union. Union has a property that does not allow any programmer to access the data types more than one.

Hence, in 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, a basic structure of union is displayed. It is the way a programmer creates the union. If you want to access any data type in the union, well you need to use it. operator similar to the structure.

Union union_name objname; With the above statement you can create the object that will help you to access any datamember in the union using the dot operator.

Union in C 1
Union in C

Main Differences Between Structure and Union in C

  1. A structure is a user-defined data type that stores the data types of different kinds. Also, it is used to represent a collection of data types’ value. If a programmer needs to define a union, then a programmer needs to use keyword struct. Whereas a union needs, a union keyword for definition.
  2. In a structure, all the data types are stored in a different location and you can access multiple data members. Whereas in union all data members share the same 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 in case of union, only the first member can be initialized.
  4. Structure’s total size depends on the sum of the size of all the data members, whereas in the case of union, the size is equal to the data member whose size is largest among all.
  5. In structure, every data type has different memory allocation, whereas in union, there is a single memory allocated for the largest size data member.

 

Infographic

Structure vs Union


 

Conclusion

Structure and unions are both user-defined data types used in c programming. Structure and Unions share the same concept of storing multiple data types. And all the data types in structure and union are accessed with a dot operator.

Structures and unions only differ in terms of size and memory allocation, but the use case of them is the same. Both of them increase code reusability and much more. If you want to store many values for a single data type, structure and union would be useful.

A basic use case of structure and union would be when you need to store a worker’s record. A structure or union will store all the data in structure or union, and then you can store or access the data. The main difference is all about memory allocation and data type access.


 

Word Cloud for Difference Between Structure and Union in C

The following is a collection of the most used terms in this article on Structure and Union in C. This should help in recalling related terms as used in this article at a later stage for you.

Difference Between Structure And Union in C
Word Cloud for Structure and Union in C

 

References

  1. Array of structures in C – Fresh2Refresh
  2. Structures and Union – C Tutorial | Intellipaat.com
  3. Unions in C Programming: Definition & Example