In this case, I am explaining explicit type casting which allows the programmer to convert or typecasts datatype from int to float as well as from float to int.
float to int typecasting means that we are converting data of type float to int as illustrated by the following program;
#include <iostream>
using namespace std;
int main ()
{
float a = 5.4;
int x = static_cast <int> (a);
cout << " value of x = : " << x;
return 0;
}
int to float typecasting means that we are converting data of type int to float as illustrated by the following program;
#include <iostream>
using namespace std;
int main ()
{
float a;
int b = 10;
a = (float) b;
cout << "value of integer datatype: "<<b<<"\n";
cout << " The value of float datatype a : "<< a <<"\n";
return 0;
}
Comments
Leave a comment