Example & Tutorial understanding programming in easy ways.

What is type casting in C++?

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




Read More →