“In typecasting, the compiler changes the datatype of our variables by itself based on how we want the program to run. C operations can only be performed with similar datatypes. If datatypes become different, then, in that case, typecasting will occur.
In C Language, we define variables or constants with specific datatypes, if after the declaration of our variables, we are writing an expression or performing some operation, and we may get the output from it in a different type. So to maintain the type, we need typecasting for that purpose. Let’s suppose we are taking two integers and dividing them by each other; the result may be in a double datatype because of the points value. So to resolve the issue, we will use the concept of typecasting.
There are two types of typecasting one is Implicit and the other one is Explicit.
In Implicit Typecasting, the process is done by the compiler, and no information is lost. In Explicit Typecasting, we as a programmer will do the casting manually, and in doing so, we might lose the information.”
Syntax
The syntax for typecasting is as follows:
If we look at the syntax, we will see that the typename is defined in the bracket before the expression or the operation. For example: If we are running an expression on integer datatypes that will give us output in double, we will write double in the type_name bracket, and after that, we will write the code for our expression. One thing to keep in mind is that mostly during the arithmetic operations, the compiler does the casting itself, which means that it is Implicit Typecasting because it is done by the compiler automatically. The compiler defines the hierarchy of the arithmetic datatypes and then does the conversion to the datatype, which is at the top of the hierarchy.
Example 1
Let’s have a look at the following piece of code to understand how we can take the help of typecasting process to divide one integer into another and get a float datatype in points. In this example, if we do not perform casting in our program, we will get output as an integer even if we declare the z variable as a double, which is used to store the value after the operation. So it will not give us the exact value, and the output will not be correct in integer.
Now in the code below, we have initialized two integers, “x” and “y,” and assigned them with values “21” and “4”. We also have taken the third variable of datatype double to store the output value, which will be in points. During the operation, we have written double in brackets before the division between x and y. This is typecasting. We are telling the compiler to cast the values in double and then divide them so that we can get our output in points. Then we printed our output to check whether it was correct or not.
There are two types of typecasting one is Implicit, and the other one is Explicit. Typecasting in C is discussed in this article.
{
Int x = 21, y = 4;
double z;
z = (double) x/y;
printf(“output after Typecasting is : %f\n”, z );
return 0:
}
After compiling our code, we will get the following value as our output. We can see that the value is in decimals and is giving us the exact value even after the point. If we haven’t performed casting on our expression, we will get the result as 5.000000 because the system will divide the integers and will not get the output after the points. But with the typecasting, we got 5.250000 which is the actual value. One thing you will notice about the code and output is that the precedence of the type_casting operator is more than that of the division or “/” operator. This means that the system will perform the typecasting first on the Integers, convert them into double, and then perform the Division process.
Example 2
We discussed earlier that there are two types of typecasting. In the previous example, we did Explicit casting. In this example, we will do Implicit Typecasting, which means that we will let the compiler do the casting by itself. We have defined 3 variables a, b and c. We have given datatypes, floats, and integers to them. A value in decimal points is given to the float variable, and a simple number is assigned to the integer constant. The variable b, which is an integer, will be converted into float automatically, and it is a bigger datatype in C comparatively. The variable a is already afloat in terms of its types.
There are two types of typecasting one is Implicit, and the other one is Explicit. Typecasting in C is discussed in this article.
{
float a = 25.23
int b = 5;
float c;
c = a/b;
printf(“output after Typecasting is : %f\n”, c);
return 0:
}
The output value will be 25.23/5=5.046. We can see in our code that we have not assigned any (type_casting) operator to our expression, but we got the value in points even with the Integer datatype. It is because the system has converted the datatype by itself. After all, the float datatype has a higher preference than the integer, and during the operation, the “a” variable was float. So datatype float was given priority by the compiler, and it automatically gave the result in float. This is called implicit typecasting.
Example 3
We cannot only convert int to float or double, but we can convert characters to other types as well. To do so, we have taken three variables with datatypes int and character. We have assigned the value “10” to our integer “x” and “Z” to our character “a”. We have summed up the character and Int variables. Logically it is not possible, but with the help of typecasting, we can do it.
There are two types of typecasting one is Implicit, and the other one is Explicit. Typecasting in C is discussed in this article.
{
Int x = 10
char a = ‘Z’;
int sum;
sum = (int) x + a;
printf(“Value of alphabet Z is is: %f\n”, a);
printf(“The total sum is: %f\n”, sum )
return 0:
}
In ASCII, the value of Z is 90. We have printed the value of z, which is 90. After adding 90, which is the value of Z in int, with the value 10, which we assigned to our x constant, we will get a total of 100. This is an example of converting a string or character datatype to a number or integer datatype.
Conclusion
In this topic, we discussed Typecasting and explained to you how typecasting works and when to use typecasting. We also discussed different types of typecasting in detail with examples. Typecasting is a very efficient approach to convert the results of different datatypes according to our requirements. It helps us to get the exact accurate information without the risk of losing our information. Another major advantage of Typecasting is that it makes our code lightweight and efficient.