Whatever we studied about the structure and whatever is explained after this topic about structure is the same for union. We can use union, wherever we are using structure but there are some differences between union and structure and those are explained below. Below differences we have to keep in mind while making a program using structure or union.


Difference of








Syntax of both is same but wherever we were using struct keyword n syntax, we have to use union there


Storage space of all members



All members of a structure having their individual storage space in memory while members of the union share the same memory storage space


Memory occupied

sum of individual memory occupied by all members of the structure

Memory occupied by that member who is occupying highest among all members

Ex. both have 2 members: one is int, another is char

Memory occupied by structure = (memory occupied by  int)+ (memory occupied by  char)

Memory occupied by union = (memory occupied by  int): because max memory occupation among int, char is int


Members can be accessed at a time

More than one

Only one

This means in a union, if we have assigned a value to any member and then we assign some value to another variable then this next variable will work properly but the variable to which we have assigned value previously, that will have some garbage value or some unwanted value. Because in the union, all members share the same storage space.

Syntax of union is the same as structure but the keyword is union instead of struct. It is as below.

union <union name>{

<data type 1> <member variable name 1>;

<data type 2> <member variable name 2>;

//and so on



union student{

int roll_no;

char name[20];

float percentage;

Example program to Prove the above points.

To find the size of any variable there is the sizeof operator. Below will return the size of a variable.

               Syntax : sizeof(<variable name>)

In the below example, there is a structure named str. It has 2 members: a (of int type), b (of char type). The variable of structure is str1. And there is a union named un. It also has 2 members: A (of int type), B (of char type). The variable of union is un1.
Now using this program, we are going to understand all 4 differences (which were mentioned in the previous table) between structure and union.

In the program first we are giving values to all structure and union members manually and then we are printing the size of both structure and union variables. Then we are printing values of members of both structure and union.

struct str{		//Diff no.1
  int a;
  char b;

union un{		//Diff no.1
  int A;
  char B;

void main()
  printf("size of structure - %d\n",sizeof(str1));		//Diff no.3
  printf("size of union - %d\n",sizeof(un1));		//Diff no.3
  printf("structure : %d %c \n",str1.a,str1.b);
  printf("union : %d %c \n",un1.A,un1.B); 	  	  //here 1st variable will show garbage value______Diff no.2,4


Learnings from this blog:

1 ) What is a union?
2) How to find the size of any variable.
3 ) Similarity between structure and union.
4 ) Difference between structure and union.
5 ) How to make a program using union.

Notify of
Inline Feedbacks
View all comments