Type Conversion in C
The type conversion process in C is basically converting one type of data type to other to
perform some operation. The conversion is done only between those datatypes wherein the
conversion is possible ex – char to int and vice versa.
There are two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
Implicit Type Conversion
This type of conversion is usually performed by the compiler when necessary without any
commands by the user. Thus it is also called "Automatic Type Conversion".
All the data types of the variables are upgraded to the data type of the variable with largest
data type.
#include<stdio.h>
int main()
{
int x = 10;
char y = 'a';
x = x + y;
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Output:
x = 107, z = 108.000000
Example 1
int a = 100;
double b = 12.5;
a + b;
Example 2
char ch='a';
int a =13;
a + ch;
Example 3
char ch='A';
unsigned int a =10;
a * ch;
2. Explicit Type Conversion
The type conversion performed by the programmer by posing the data type of the
expression of specific type is known as explicit type conversion. The explicit type
conversion is also known as type casting.
Lower
Higher Data
Data
Type
Type
#include<stdio.h>
int main()
{
double x = 1.2;
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output: sum = 2
Example:
double da = 4.5;
double db = 4.6;
double dc = 4.9;
int result = (int)da + (int)db + (int)dc;
printf("result = %d", result);
Output
result = 12