Type Casting and Conversion in C
In a programming language, the expression contains data values of similar datatype or different datatypes. When the expression contains similar datatype values then it is evaluated without any problem. But if the expression contains two or more different datatype values then they must be converted into single datatype of distinct datatype.
Here, destination means the location where the final value of that expression is being stored. For example, when we multiply an integer type data value with float type data value and store the result into a float variable.
In this case, the integer type values must be changed
into float value so that the final result is a float datatype value.
In c programming language, conversion of data is executed by two(2) different
methods.
Both are:-
- Type Conversion &
- Type Casting
1)Type Conversion
The type
conversion is theway to convert a data value from single datatype to multiple
datatype and vice-versa automatically with the help of compiler. Sometimes type
conversion is also called as implicit type of conversion. The implicit
type conversion is automatically performed by the compiler.
For example, when we
assign an integer data value to a float variable, the integer value atomically
gets converted into float data value by adding decimal value 0 in C language.
And when a float value is assigned to an integer variable the float value
automatically gets converted into integer value by removing the decimal value.
To understand more about Type Conversion let us observe the following example…
int i = 10 ;
float x = 15.5 ;
char ch = ‘A’ ;
i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i
x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to
variable x
i = ch ; =======> Here the ASCII value of A (65) is assigned to i
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int i = 95 ;
float x = 90.99 ;
char ch = ‘A’ ;
i = x ;
printf(“i value is %d\n”,i);
x = i ;
printf(“x value is %f\n”,x);
i = ch ;
printf(“i value is %d\n”,i);
}
Output:
i value is 90
x value is 90.000000
i value is 65
In the above program, we assign i = x, i.e., float variable value is assigned to integer variable. Here, the compiler automatically converts the float value (90.99) into integer value (90) by removing the decimal part of the float value (90.99) and then it is assigned to variable i. Similarly when we assign x = i, the integer value (90) gets converted to float value (90.000000) by adding zero as decimal part.
Type Casting
Type casting is also called as explicit type conversion. Compiler converts data from one datatype to another datatype implicitly. When compiler converts implicitly, there may be a data loss. In such situation, we convert the data from one datatype to another datatype using explicit type conversion. To perform this we use the unary cast operator. To convert data from one type to another type we specify the target datatype in paranthesis as a prefix to the data value that has to be converted. The general syntax of type casting is as follows…
(TargetDatatype) DataValue
Example
int totalMarks
= 450, maxMarks = 600 ;
float average ;
average = (float) totalMarks / maxMarks * 100 ;
In the above example code, both totalMarks and maxMarks are integer data
values. When we perform totalMarks / maxMarks the result is a float value, but
the destination (average) datatype is float. So we use type casting to convert
totalMarks and maxMarks into float datatype.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int a, b, c ;
float avg ;
printf(“Enter any three integer values : “) ;
scanf(“%d%d%d”, &a, &b, &c) ;
avg = (a + b + c) / 3 ;
printf(“avg before casting = %f\n”,avg);
avg = (float)(a + b + c) / 3 ;
printf(“avg after casting = %f\n”,avg);
}
Output:
Enter any three integer values : 10 20 30
Avg before casting : 20.000000
Avg after casting : 20.000000