Interested in Personalized Training with Job Assistance? Know More
Hello everyone, my name is Aditya and we are continuing with the C programming course.
in which in the last video we had discussed our fifth derived data type, which was union.
We had seen what exactly is union? And we got to know that union is basically a group in which we put different data type variables and use any of the variables at a time with the help of the Union name.
And now after seeing the derived data types, we are going to see such a function which helps to change our data type’s name.
Here we are talking about typedef function.
So, how does this typedef work? How would be its exact definition? We will see all these things in this video.
Without any delay we will start and we'll see what is typedef.
So, typedef is used to provide some meaningful names to the already existing data types.
So, the data types which are there in our program, if I say that I only want to use the integer, I might use the integer’s name as it is.
Which means I will use int as int.
But many times it so happens that our data type, its name becomes a little longer like I have to keep signed with that I also have to keep integer and they also have to include long.
Like I have to keep signed, along with that I have to keep integer as well and I have to include long as well.
So, in this way our data type’s name becomes longer and if I want to use the same data type constantly.
So, I will have to use this long name in my program constantly.
Sometimes it happens that our data types, like if I take a float, in the scenario of the program, such a situation comes, where I have to represent float value with a certain name.
So, what will I do? I will change the name of the float data type, only for that program.
I will change the name and give such a name which would be logically matching to my scenario and it is helping to make my program effective.
So, basically typedefs function work is that if you're using any data type, you change its name and give such a name whose effectiveness is more in your program.
This is what we are basically going to do with the help of this function.
So, we have seen the definition.
Then after that, we saw what exactly does typedef do.
Now we will see its syntax.
How can we change the data type’s name and give a new name which means how we can change the name? This is what we will see with this syntax.
Since it is a typedef function, so basically, I will use a keyword, whose name will also be typedef.
So, with typedef, my syntax will start and in front of typedef will be my data type’s name.
And in the same way we had made one more preprocessor command which was starting with #define.
So, we will see how it is different from #define.
But for now, we will focus on typedef syntax.
So, you will see that after writing the typedef keyword, whatever is the existing name.
We will write whether it is signed or unsigned data type, integer or float.
In this way, we will combine different data types and make one name.
And after that we will give space and the new name that we have to give to our data type.
Which means we will be naming an alias.
That name will come at the end of our syntax.
So, this basically you can use anywhere in the main function.
We will see how this can be written.
So, we will move ahead and see how we can write this.
So now here in front of you there is one example, where we are making use of one typedef function.
So basically, what is it that we are doing here? So, this i n t which is our data type of integer, we will change its name and give it a new name.
So here in the syntax, you can see we have used typedef keyword and after that i n t or integer data type’ name is written.
And what do we have to convert it with? We have to convert it with a vector name.
So, basically when I want to use integer type data type in my particular program.
So, what will I do? I will be calling it with the name vector.
After this definition, whenever I will write this line, I will complete it in my program.
After that whatever is made integer, it will become my vector.
And we will be calling it by the name of vector.
Here we declare vector as a new name to the type int with the help of typedef function.
So, this is how we use this typedef function.
Now how will its functionality work? We will see it now.
Now we can create variables of type vector in the following ways.
So, now what we have done from typedef, we have made it into the vector.
So, whenever I have to make an integer type variable like you can see in the example over here.
So, instead of writing int, I will write vector.
Here in the example, we have written a vector and after that the integer type variable that we had to make x, y and z.
All three I have given a vector name data type.
So, what happens with this? We don't use the integer and we use some new name which helps us more in building the scenario.
So, here I will not write int x, y, z, I will write vector x, y, z because we had seen in the last slide with the help of typedef function, we had converted int into vector.
So, in this way, our accessing the variables are there or the thing of definition of a typedef function, it works in somewhat this way.
Now we had discussed our #define preprocessor commands in the last few videos.
In these #define preprocessor processor command, we were doing the similar things.
We had changed the data type’s name and given it some new name because of which it is easy for us that we don't have to write such long names.
So, this is the typedef that I'm talking about.
So, how it is different from #define, what is the basic difference between these two? That is what we will see now.
So, the first thing that we will talk about is the processing of these two, how they are processed.
So, we know that the command that starts with a hash, we call them as preprocessor commands.
And this preprocessor command is #define.
And that’s why this compiler doesn’t process.
Who process this? This is a preprocessor command so the preprocessor processes it.
So, the compiler basically reads our main program.
And before that the program starts with #, that is seen by our preprocessor, it reads it and imports it.
So, here if we talk about typedef.
Our typedef is read by the compiler as we write it in the main function.
So, this is the difference in who processes it.
Now we will see its main difference.
Now we will see the main difference, where all the functionality works and in which situation it works.
So, if we talk about typedef, it is limited to giving names to types only.
So, the type def helps us to only change the names of data types.
But if we talk about #define.
So, #define can also be used to define other things.
Like you can define 2 as 2.
So, what you can do with #define basically, you can change the names of normal data types.
Along with that, there could be such names and values, which you have to call by some other name in your program, so you can even change and use that.
Like you have used #define and you basically wrote the normal numeric 2 as two.
So, whenever you want to print numeric 2, you will write capital T W O and your compiler will understand that here it is already defined.
So, in place of two it will print numeric 2.
So, the difference is that in typedef, you can only change the data type.
With the help of #define you can change anything.
So, in this way, these are the differences between typedef and #define.
But both their functionalities are used in different places.
All these things as and when we will practice the program, we will get to learn this slowly.
Let’s move ahead after seeing our typedef and #define function.
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 talking about input and output functionalities.
So, we have seen about input and output early on, which is our printf and scanf statement, how it provides input and output.
So, what will be its effectiveness?
We will be seeing all these things in our next video; till that time you can practise our typedef and #define function and we will meet in the next video with input and output functionalities’ function.
Till that time, thank you so much.
Have a friend to whom you would want to share this course?