Hello everyone, my name is Aditya and we are continuing this C programming course.
in which in the last video, we had discussed structures.
We had seen why exactly these structures are used and after seeing so many derived data types, what was the need that we had to see one more derived data type which is called structure.
All the answers to these questions we had seen in the last video.
Now, it is time to discuss unions used in C programming.
Now, we are going to see our last derived data type which is an extremely efficient and important data type.
We will see about it and we will try to understand what is the use of this union here, why do we need Union.
Now, we have seen so many derived data types arrays, pointers, strings and along with that we have also seen structures.
So, what is the need of this union? We will get to know this in the next slide.
In the next slide we will see what unions are.
Now the answer to this is very simple.
Unions can use the same memory for multiple purposes.
Which means, the location of one memory, we can use the same as an integer, as a float or as a string.
So, how will we use it? We will use it with the help of the unions.
Further in this slide we will discuss how that can be done, till that time we can read this, union can store different data types in the same memory location.
Even this we have seen, after that they are saying unions have many members or numbers but only one member can have a value at a time.
So, now we will make one union.
So, union is extremely similar to the structure.
In the way, the structure has different members, which means there are variables with different data types.
But what is the condition over here? Union basically uses the same memory in different places.
What will we do in this? We have kept integer type variables in our union, we have kept a float type variable and along with those strings have also been kept.
So, if I want to use an integer, so along with the union I will access an integer member.
And I will use it in my program.
If I want to use float sometimes, with the same union I will access float union.
And the integer one will be removed from there, from that memory and we will only focus on the float member.
So, in this way as many as members we make in one Union.
At a time we can use only one member So, what will happen with this we can access different types of things and different types of members, we will understand it deeply.
We will go ahead and see how we can define the union; in which way we will write its definition.
Firstly, like in every definition we say even in this definition, we will start with the keyword and this keyword is union, u n i o n, with this union keyword we will start our union, after that we will write the union’s name.
The way we were writing the structure’s name.
In the same way we will write the union's name, the flow will remain exactly the same.
But the only difference here will be the keyword.
So, the keyword here is union and in structure it was struct.
So, here the union is a keyword.
So, the compiler understood that this is a union and not a structure.
It will treat it as per the functionality of union.
So, what is the functionality? Like always different members of different groups will be there.
And like we make the variable.
Variables will also be made, you can see here in the syntax, like var 1 var 2, going ahead I can make more such variables on the basis of particular union.
So, this was about the definition, how the union can be defined.
Now we will go ahead and see how we will access the members inside it? Again, it's the same way.
Like in our structures we were using member access operators.
We were accessing our members in the same way in the Union.
The same procedure will be followed.
And if you talk upon the syntax, the union data type variable that we had made, which means the union variable that we will write in front of it we will put member access operator and after that whatever is the name of the Union, whichever member in it we wish to access.
We can write its name.
So, in this way we can access the members so again, here we are understanding all the things theoretically.
In the coming videos we will be seeing them practically as well quickly.
We will go ahead and straight away go to the text editor which is our Visual Studio Code.
Going to the Visual Studio code, we will see how a program’s union works and how the functionality of the Union works.
So here are the first two commands; these are preprocessor commands.
Since we are using strings over here we will import the string.h file as well.
If we see ahead, before the main function that we use to make a structure which was made with struct keyword.
Similarly, here we have made a union with the keyword union.
So, this union’s name data which means this data starts with a capital D and if you see ahead of it, there are basically three members.
First member is integer type I variable and the second is float type f variable and third is our string which is called ch.
So, these three are our members.
So here you can see between these we have not made any variables, which means that going inside the main function we will be making a variable which would be of union data type, we will be making that.
So, you come into the main function.
And you can see that here union data, we have made a variable of a particular date type, which is called d.
So, d has become our variable.
With the help of d.i, we can access every member and one by one.
So, what are we doing here? I have made d, one variable and with the help of d.i, this I member for d variable, I have accessed it and in that I have stored the value 100.
In the same way I have written d.f and in if I have stored the value 200.5.
So, these 2 ways are done.
After that in our string with the help of string function, with the string copy function, we have basically copied C programming, this string.
Where was it copied in? In ch string.
So, all these three initializations that we have done, it is not showing any error in the programme and very easily it is getting initialised over here.
In the way the structure’s variables were getting initialised Even here they are getting initialised.
So, the main difference that will come is that you'll be able to see in printf statements.
Where we would be initialising all the three things together and after that, we will be printing all three with the help of printf.
So, what is the difference that we will be able to see? Let’s see that.
So first of all, when we are going to the d.i, what will come? When we will be printing, d.f, then what will come? Similarly, when we are printing our string what will come? So, we will directly search all the answers to these questions by running our code.
Here you can see that as soon as I have run the code, all three statements have been printed.
But has it been printed in that particular order, in which we wanted it? We would see that; what we had done was, we had put 100 in d.i and when we had printed it here, with the help of printf, to d.i.
You can see that here there is a weird value that got printed.
100 has not been printed over here.
So, why has this weird value been printed? Because if you will see after initialising this particular variable, we had also initialised f.
And we have learned this union property, at a time, only inside one member value will be stored, that is the reason why it is also called as efficient data storage and our member of the biggest size, its size will be occupied by the union.
And whenever we will be doing a new initialization with the new member, so the previous member’s initialization will get cancelled out.
It will become corrupt and so here when d.i’s value is getting printed.
So, one corrupted value is getting printed over here.
We had written 100.
But since even after we have done the initialization in f because of it what happened? The data in i got corrupted.
After that if we see, in f we had stored 200.5 Even its value has come weird in corrupted form.
This means that after f, we have located again in this string, we have also inserted value in that.
So, what happened because of this, our f’s value also got corrupted.
Basically, what happens in the union, the initialization which has happened at the end, only that matters and only that is important, that's why at the end with the help of string copy, we had stored C programming in our string.
That C programming is printed as it is in the correct form with the help of the d.ch.
So here you will see what happens in the union.
Different members are made but we can use one member only at one time.
And what happens with this? We get to access different data types variables and different data type members, in the same name.
So, this is the functionality of the Union.
That's the reason why union is different from the structure.
So, with the help of different requirements, we can make particular union programs and we can practise it.
With which the requirement or the solution that we want, will be able to come to us more effectively.
So here we have seen how union should be practically used and created.
How we can access the values and members inside it.
We have discussed all these things in this video.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
In the next video, we will be discussing typedef function.
What is it exactly? In this video we have discussed unions.
Even you can practise unions till that time, we will meet in the next video.
Till that time, thank you so much.
Share a personalized message with your friends.