Typecasting in C Programming Language
In this lesson, we will learn about what is Typecasting and different types of it. We will also go through some examples and take a quiz at the end.
What is Typecasting
In C program the process of changing one data type into another data type is known as Typecasting. There are two types of typecasting:
- Implicit Typecasting
- Explicit Typecasting
Generally when we add, subtract, multiply, or divide two or more values of similar data type the output is also of the same data type. For example:
int value / int value = output will be an int value
float value / float value = output will be a float value
double value / double value = output will be a double value
int a = 10;
int b = 5;
int c = a/b; // the output of c is 2 which is of int data type
In typecasting, when we work with dissimilar data types, the output can be a different data type. Let's explore the explanations of each type of typecasting with examples and how to use them in programming.
Implicit Typecasting
This is also known as automatic type conversion. In this type of casting (conversion) the data is converted from one data type to another data type by the compiler itself without programmer interference. This usually happens when we assign a smaller data type to a larger data type.
int num = 10;
float result;
// Implicit type casting: int to float
result = num;
In the above example, when we assign the integer variable num to the float variable result, the compiler will automatically converts the integer value into a float value because the int datatype is smaller than float so it can be easily stored in float.
Let's see some more examples.
int value / float value = output will be of float datatype
float value / int value = output will be of float datatype
int value / double value = output will be of double datatype
double value / int value = output will be of double datatype
float value / double value = output will be of double datatype
double value / float value = output will be of double datatype
Whenever an equation contains two or more different data types, as shown in the above examples, the result will always be in the data type which is largest amongst the data types involved in the equation. This is because all other data types automatically convert to the highest data type available in the equation, resulting in the final output being of the highest data type. This type of conversion is known as Implicit Casting.
Explicit Typecasting
This is also known as manual type conversion. In this type of casting (conversion) the programmer manually converts a variable from one type to another using the cast operator. The syntax for explicit type casting is:
(datatype_name) expression
float num = 5.67;
int result;
// Explicit type casting: float to int
result = (int)num;
In this example, the float num is explicitly cast to an integer when assigned to result variable. As a result, the fractional part of the float is discarded, and only the integer part is retained.
Whenever we want to change the data type of a variable using explicit casting method, just write the name of the data type within ( ) before the variable name we want to convert, as shown in the example above.
Let's see one more example for more clarification.
int a = 5;
int b = 2;
float result;
// Explicit type casting: int to float
result = (float)a/b; // result will be 2.5
In the above example, the variables a and b are of int datatype. So if we divide then without typecasting the result will be 2.0 which is wrong. If we want the correct result, then we have to cast any one of the integer variables (a or b) to float using the cast operator as shown in the above example.
Changing the data type of the variables a or b using explicit casting within the equation does not alter their original declared data types. The original data types remain unchanged. The changes are temporary and only apply for the purpose of solving the equation.
float c = 10 / 7; // result of c will be 1.0
In this case, the result is 1.0 because both numbers are treated as integers. To get a more accurate result, we can change either of the numbers to a decimal by adding ".0" to the end of the number:
float c = 10.0 / 7; // result of c will be 1.428571
Or we can do it like this:
float c = 10 / 7.0; // result of c will be 1.428571
Adding ".0" makes the numbers float, which gives us a more precise result.
Test Your Knowledge
Attempt the questions in the exercise to check whether you have understood the lesson clearly.