Type casting in C++ by R4R Team

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;
}


output-

x is 107
z is 108.000000


Explicit Type conversion:
Lower data type->Explicit conversion -> Higher data type

program:

#include< iostream>
int main()
{
double x=3.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
cout<<"sum="<< sum;
return 0;
}


output-

sum=4




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!