Structure vs Union: Difference and Comparison

Structure and Union are used in programming to facilitate the function of variables, the flow of data, and the ability to manipulate data. Handling variables is a very important process, as that helps us define complex algorithmic structures that can process data.


IT Quiz

Test your knowledge about topics related to technology

1 / 10

What does AM mean?

2 / 10

Which two websites offer free e-mail services?

3 / 10

Which of the following is not an electronic device?

4 / 10

Which of the following semiconductor is mostly used to construct electronic circuits?

5 / 10

The core idea of develop AI is bulding machines and alogrithms to

6 / 10

Phones that offer advanced features not typically found in cellular phones, and are called

7 / 10

Who founded Microsoft?

8 / 10

Which of the following most advanced form of AI?

9 / 10

How many numbers of home pages a web site can contain

10 / 10

With reference to a computer network, the exact meaning of the term VPN is

Your score is


In the same way, the programming language C offers two variable types: structure and union.

On the face of it, they both seem to possess pretty similar functions and in some cases, they can be used for the same function too! However, there are several differences that these possess, knowing which helps differentiate these terms.

Key Takeaways

  1. A structure is a collection of variables of different data types, while a union is a collection of variables that share the same memory location.
  2. A structure allows for multiple variables of different data types to be grouped, while a union allows for different variables to occupy the same memory space.
  3. Structures are used for data organization, while unions are used for data optimization and saving memory space.

Structure vs. Union

A structure is a collection of related variables of different data types that can be accessed using dot notation. A union is a data type that allows multiple variables to be stored in the same memory location, with only one member being used at a time. It is useful for conserving memory when working with variables of different data types.

Structure vs Union 1

Comparison Table

Parameters of ComparisonStructureUnion
DefinitionStructure allocates different items to different memory locations.Union \allocates different memory items to the same location.
Syntaxstruct struct1{Data type 1;Data type 2;…}variable 1, variable 2,…;union union1{Data type 1;Data type 2;…}variable1; variable2, …;
Keywordstruct union 
Memory All data types are in separate memory locations.All data types are in the same memory location.
SizeStorage size is the sum of the size of all data items.Storage size is the value of the largest data type.

What is Structure?

Structures are the variables that can hold many types of data items at the same time. It is, however, worthwhile to note that the data items held by a structure can be of different data types.

Thus, the structure is a very useful way to store, access, and manipulate data. A structure is defined using the ‘struct’ statement. To understand what a keyword is, we will have to look a bit into the concept of the language.

In any programming language, not just C, we need to use a keyword or a command that helps the compiler recognize a certain function that has been called, and it will execute accordingly. 

The syntax of a structure follows the following pattern:

struct structure_name{

char firstName[10];

char lastName[10];

Int age;

char address[20];


This structure has been named ‘structure_name’ and can be called anywhere in the function after it has been defined. It can be called several times also, as per requirement. We can see that the structure has a few data items as its attributes.

They are the first name of a person, the last name of that person, their age, and their address. Notice how there is more than one data type present in the structure. All these data types will have separate memory locations where they will be stored. 

By default, all the members in a structure are public. By public, we mean that all other functions and objects can access all the data types in the structure. This property can, however, be changed to private to protect the data.


What is Union?

A union is a data type storing various data items inside. This is found in the programming language C, which helps insert, manipulate, and access data in a program.

It is, however, worthwhile to note that unions can not hold data items of different data types, which limits their capabilities compared to structures to some extent.

When we define or call a union, it is pretty much the same method the way we call a structure. The keyword for union is ‘union’ only, followed by the name of the union the programmer wishes to give.

The syntax of union is as follows:

union union_name{

Data object 1;

Data object 2;



Similar to structures, the union, after being defined, can be used anywhere in the function, other classes, and objects. The union can be used as many times as required.

The memory space occupied by the union will be the same as the memory required to accommodate the largest data type mentioned in the union. 


Main Differences Between Structure and Union

  1. The main difference between Structure and Union is that structures are variables that can accommodate other variables and allocate them separate memory spaces. In contrast, unions allocate the variables to the same memory space.
  2. The structure keyword is ‘struct,’ while the union keyword is ‘union.’
  3. Structures can store multiple values simultaneously, while unions can store only one value simultaneously.
  4. Structures can help to view a single memory location in only one way. Unions help to view a single memory location in many ways.
  5. A structure can not be anonymous. However, a union can be declared anonymous.
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!

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.