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.
No. |
Difference of |
Structure |
Union |
Explanation |
1 |
Keyword |
struct |
union |
Syntax of both is same but wherever we were using struct keyword n syntax, we have to use union there |
2 |
Storage space of all members |
Different |
Same |
All members of a structure having their individual storage space in memory while members of the union share the same memory storage space |
3 |
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 |
4 |
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.
#include<stdio.h> #include<conio.h> struct str{ //Diff no.1 int a; char b; }str1; union un{ //Diff no.1 int A; char B; }un1; void main() { clrscr(); str1.a=1,str1.b='x'; un1.A=2,un1.B='y'; 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 getch(); }
Output:
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.