Hello everyone, my name is Atharva and we are continuing this C programming course in which in the last video we had discussed preprocessors and header files.
We have seen what exactly is a preprocessor? How different is a preprocessor from a compiler or is it a part of the compiler? Or is it completely different from the compiler, all these things we had discussed in the last video.
We got to know that in these preprocessor directives are used which are #include, #define and #undef and different types of preprocessor directives.
So, we have seen the directives.
After that we have seen how our header files in which the function definition is written which we are using in our program, how those header files are imported with the help of various preprocessor directives.
All these things we got to know about preprocessors and header files in the last video.
After knowing all these things, now is the time for the new topic and it is called typecasting.
If you want to understand typecasting very easily, we will take an example, like we make the variables in our C program, what do we do in that is we declare the variable as a data type.
Like we have made a variable named num.
What did we do with the data type, we made it as an integer.
In the same way if I want to float the number, which means I have to make it a decimal one, what I will do is I will write a float data type.
But if I want to do such a thing that at the start, I had declared the data type of that variable as an integer but going ahead the value that is coming ahead in the program, if I convert that into some other data type value that is called type casting, changing the type.
We will know about this going further and we will get to know what exactly is type casting.
So, even here you can see that there is a simple definition in which it is written “Converting one datatype into another is known as type casting” which means that is any variable of one data type variable.
If I convert it into some other data type to the same variable that is called as typecasting.
So, this is a basic definition.
Now, we have understood about it typecasting means changing the data type, whatever value is there inside it, changing its typed completely.
Like when I put integer, I converted them into float value.
And then I made some character even that character I changed into an integer.
So, these kinds of data types which are there, how we can interchange them from one, other.
How can we change their types, all these things we will see in typecasting.
Now, we will come to a basic example.
What is this example basically doing? In two variables, whichever values are stored it is making a sum of it.
So, we will see how this programme works and we will quickly understand where is this typecasting concept coming here.
So, we will start with our preprocessor command after that, we will come to our main function.
So, the first step in our main function is getting used over here.
We have made two variables one is called a and the other one is called b.
In a we have stored the value 17 And in the other one we have stored 5.
So, basically what we will do is make a sum of a and b.
So, we will make a sum here and now I have made a third variable in which basically I'll be storing the sum and I have not given this data type as integer.
I've given this data type as float.
Now we will see how typecasting will happen here.
The type will be changed of any value, type casting can be done in different ways.
Even that we will discuss quickly but for now, we will concentrate on this example and understand how the work is happening here.
So, we had made a and b variables which were of integer data type and there is a third variable in which its sum will be stored.
I have given that data type float, so here you can see that a simple calculation is performed over here.
We have added a and b and the output or the value that will come that will come, the value that will come, where have we kept that value? We have kept it in sum.
So, here there is a thing to pay attention to and if you see it carefully a was an integer.
So, the values stored in it were 17 and 17 is an integer value.
In the same way if we talk about b, even in b we have stored 5 and 5 is also an integer.
But if we add this both which means if we do 17+5 according to you what output should come, output will come as 22, 17+5=18, 19, 20, 21, 22.
So, the output is 22 and we all know that if you see 22 normally is an integer.
So, what we did was the integer value that has come 22, we stored it in a float data type variable where you can see that the sum’s data type was float.
So, in the float variable we had store 22.
So, this 22 which was the integer value, where will it get converted? It will get converted into the float value and when we will convert it in the float value, what is the float value? it is a decimal point value.
What will happen in decimal? It was only 22 and it will become 22.00.
In this way we can convert the integer value into float.
So, when we will run the printf statement over here where it is showing that addition colon is equal to whichever value will be stored under sum that will get printed over here and what is the value stored in sum, 22.00.
If the sum variable was of integer type.
So, in that after printf statement the value that it would have shown it would have only been 22.
But since here it is a float data type.
That's why it will display over here with the help of printf statement 22.00 So, here there is a difference of a point.
So, this can be done in different ways.
Like you have defined a as an integer, going ahead what you did was its value you considered as a float or you have defined it again in float.
So, these kinds of changes if we can do is we call it typecasting.
Which means any type of the value we convert it costed it in another data type.
So, this was a simple basic concept related to typecasting.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.