Changing the type of the variable is known as type casting or type conversion.
There are two type of type conversion in C
Implicit Type conversion
Explicit Type conversion
Implicit:
program:
#include< iostream>
int main()
{
int x=20;
char y="a";
//convert character to integer
// Here y is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
cout<<"x is "<< x<< endl<<"z is "<< z;
return 0;
}
x is 107
z is 108.000000
#include< iostream>
int main()
{
double x=3.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout<<"sum="<< sum;
return 0;
}
sum=4