Difference between Structure and Union in C

Structure and Union both are the user-defined data types where the user can store different data types in one at the same memory location in the form the one memory block. Both may sounds a lot similar as both of them are the same in there in nature of storing different data types, but both of them are quite different when talking about how they store these different elements in memory. Let’s first see the main differences between structure and union and after that, we will discuss them one by one.

Difference between Structure and Union in C

structureUnion
struct keyword is used to define a structure union keyword is used to define a union
Memory allocated to structure is sum of memory space assigned all elements but padding and packing can make it less or greaterMemory assigned to union is equal to size of the largest element in union
All members of the structure are accessible at any given time during program execution. Because each member of structure has a unique member space assigned to it. Only one element of union is accessible at a time and acessing other elements gives incorrect result. Because space memory space is used for all members of union.
Changes made to any member of structure does not effect the value of other structure membersOn the contrary , changing value of on union member also effects the values of all other members
nested structures are possibleNested unions are also possible
Consumed more memory consumed less memory

Structure Introduction

In C programming language structures are defined using the keyword struct and it can store one or more data elements in it. A simple structure example is shown in the code below,

struct sample  
{  
    int first;   
    char second;   
    long third;  
};  

As is obvious it is storing 3 different data types i.e. int, char and long in one place under the hood of a single structure named as sample. The question which commonly arises here is, how a structure stores these different data types in memory. The answer to this is, each data element present in a structure is allocated a different memory location of its size in memory. These data types are not sharing any memory with their sibling elements. An example code using structures in C is given below.

Structure Example Code in C

#include<stdio.h>
struct sample
{
	int first; 
	char second[20]; 
	long third;
};

int main(void){
	struct sample structure= {20, "Sample structure", 176873487};
	printf("Integer of structure is: %d\n", structure.first);
	printf("String of structure is: %s\n", structure.second);
	printf("Long of structure is: %ld\n", structure.third);
	printf("Integer: %d\nString:  %s\nLong:    %ld\n", structure.first, structure.second, structure.third);
	return 0;
}

This code shows the use of a structure in C language. The output of this code is shown below.

Output:

difference between structure and union in C structure demo output

The print statement on line number 11, 12 and 13 are straight forward as they are accessing the memory at different time. Print statement at line number 14 represents the actual use of structures, because it allows us to access all the elements present in a structure at one time. Let’s now see what the case with unions is.

Size of Structure

Now, let’s modify the above code and check the memory allocated for the structure using sizeof() operator. This code prints the size of the structure sample on the console.

#include<stdio.h>
struct sample
{
	int first;
	char second[20];
	long third;
};

int main(void) {
	struct sample structure = { 20, "Sample structure", 176873487 };
	printf("Size of structure is: %d\n", sizeof(struct sample));
	return 0;
}

Output

The output of above code shows the size of structre is 28 bytes which is exaclty the sum of all elements of structure “sample”.

Size of structure is: 28

Union Introduction

Unions are different from structures only in the way in which it stores different data types in memory. Unions are better for memory critical programs, in which we need to use as less memory as possible, because all the elements present in one union block shares the same memory location. A simple code showing the use of unions is given below in the example.

Union Example Code in C

#include<stdio.h>
union sample
{
	int first; 
	char second[30]; 
	long third;
};

int main(void){
	union sample union1= {20, "Sample union", 176873487};
	union1.first = 12;
	printf("Integer of union is: %d\n", union1.first);
	strcpy(union1.second, "Testing single member"); 
	printf("String of union is: %s\n", union1.second);
	union1.third=1537346;
	printf("Long of union is: %ld\n", union1.third);
	printf("Integer: %d\nString:  %s\nLong:    %ld\n", union1.first, union1.second, union1.third);
	return 0;
}

The output of this code explains the use of unions in C very well.

Output

difference between structure and union in C union demo output

This code example shows that when we try to access the structure elements one by one they do not give any issue, because we are accessing the same memory block for different data types. In code line number 17, when we try to access all the elements of the union in one statement we cannot do so because it is trying to read a character strings from a long type memory block.

Size of Union

Now lets see the size of union user defined data type with this code:

#include<stdio.h>
union sample
{
	int first;
	char second;
	char third;
	long fouth;
};

int main(void) {
	union sample list;
	printf("Size of list is %d", sizeof(list));
	return 0;
}

Output

The output code shows that the size of union is equal to the size of largest member of union which is long.

Size of list is 4
  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C

Leave a Reply