 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you the difference between structure and union in C programming with the help of a practical example. Before starting, you can note how you can search my YouTube channel. You can simply type my name Sanjay Gupta in YouTube. My channel will be available there. You can watch various programming related videos in my YouTube channel. Now I am going to demonstrate you the difference between structure and union. So first time including header file as tdio.h, then I am declaring a structure whose name is number. Inside the structure, I am declaring three variables. First one is int a, second one is float b and character c. So this way I have declared three different members of structure number. Now inside main function, I have to declare variable of this structure number. So this is variable or instance of structure that is n. Now through n, I can access a, b and c which are members of number structure. So first time assigning 10 into n.a, then 12.5 into n.b and a in n.c. So this way I have initialized or you can say I have assigned 10, 12.5 and a in a, b, c respectively. So structure variable n is having three different values. Now if I want to print these values on console, so I am using this statement. First one is print f, d, n.a. So here value of a will be displayed. Then to display b, I am writing n.b and to display c, n.c. This way I have implemented three print f statements which will display values of a, b, c with the help of n that is structure variable on output screen. Now let's see what happens when I will be executing this code. You can see the output. All three variables are printing proper results which are available in these variables. These values are displayed on output screen. So structure provides different memory blocks to each member which is available inside it. So here structure number is containing three variables a, b, c. So different memory blocks for a, b, c will be allocated. That's why these assigned values are assigned properly in this n variable. Now I am going to convert this structure into union. So union doesn't allocate separate memory blocks for each member. It only allocates memory to the block or to the member which is having highest amount of memory. So here in this number union, three members are available but b is having highest amount of memory. So b requires highest amount of memory. That's why only memory block which is equal to the variable b will be allocated and a, b, c will be sharing that common memory block. So separate memory will not be allocated in case of union. So now I have assigned n dot a, n dot b and n dot c with 10, 12.5 and a respectively. And again I am going to display these values. Now let's see what happens. You can see a and b both are printing garbage values but c is printing proper result. So in case of union, if you assign value into a, just after a, if you assign value into b, then value of a will be replaced and value of b will be assigned into the union. Then if you assign value into c, then b will be replaced and the value which is assigned into c will be available in union. So that's why a and b are printing garbage values because their values are overwritten and c is printing proper results. Now what is the benefit of union? If you want to union properly, then just after assigning a value to a member, you have to use that value so that new member can assign its value and previous value can be overwritten. So now I am placing these printf statements just after the assignment operation. So this way just after assignment, I have used the value of a particular member. So n dot a is receiving 10 and after that I have displayed n dot a on output screen, then b is assigned a value, then it is displayed, then c is assigned a value and it is displayed. So this way just after assignment of a value, I have processed that value and then I have assigned new value to another member. Now it will show all three values properly. You can see the output. This time a, b and c, all three variables are printing correct results. These are printing the values which are assigned in these variables. So I hope you have understood the difference between structure and union, how union works and how structure works. So in case of structure, you can use all the memory blocks at same time but in case of union, you can use only one member at a particular time because memory location is shared with all the members which are available in a union. I hope you have understood the differences properly. If you want to watch more programming related videos, just type my name, Sanjay Gupta in YouTube. You will find my channel, watch various programming related videos there. Thank you for watching this video.