C++ Type Conversion

Type conversion is the process of converting a data value from one data type to another data type.

In the C++ programming language, the data conversion is performed in two different methods and they are as follows.

  • Implicit Conversion (Type Conversion)
  • Explicit Conversion (Type Casting)

Implicit Conversion (Type Conversion)

The type conversion is the process of converting a data value from one data type to another data type automatically by the compiler. Sometimes type conversion is also called implicit type conversion. The implicit type conversion is automatically performed by the compiler.
For example, in c programming language, when we assign an integer value to a float variable the integer value automatically gets converted to float value by adding decimal value 0. And when a float value is assigned to an integer variable the float value automatically gets converted to an integer value by removing the decimal value. To understand more about type conversion observe the following...

int i = 10 ;
float x = 15.5 ;
char ch = 'A' ;

i = x ; =======> x value 15.5 is converted as 15 and assigned to variable i

x = i ; =======> Here i value 10 is converted as 10.000000 and assigned to variable x

i = ch ; =======> Here the ASCII value of A (65) is assigned to i

Example code
#include <iostream>

using namespace std;

int main()
{
    int i = 95 ;
    float f = 90.99 ;
    char ch = 'A' ;

    i = f ;     //float to int  --> 90.99 to 90
    cout << "i value is " << i << endl;
    f = i ;     // int to float --> 90 to 90.000000
    cout << "f value is " << f << endl;
    i = ch ;    // char to int  --> 'A' to 65
    cout << "i value is " << i << endl;

    return 0;
}

Explicit Conversion (Type Casting)

Typecasting is also called an explicit type conversion. Compiler converts data from one data type to another data type implicitly. When compiler converts implicitly, there may be a data loss. In such a case, we convert the data from one data type to another data type using explicit type conversion. To perform this we use the unary cast operator. To convert data from one type to another type we specify the target data type in parenthesis as a prefix to the data value that has to be converted. The general syntax of typecasting is as follows.

(TargetDatatype) DataValue

Example

int totalMarks = 450, maxMarks = 600 ;
float average ;

average = (float) totalMarks / maxMarks * 100 ;

In the above example code, both totalMarks and maxMarks are integer data values. When we perform totalMarks / maxMarks the result is a float value, but the destination (average) datatype is a float. So we use type casting to convert totalMarks and maxMarks into float data type.

Example code
#include <iostream>

using namespace std;

int main()
{
    int a, b, c ;
    float avg ;
    cout << "Enter any three integer values : ";
    cin >> a >> b >> c;

    avg = (a + b + c) / 3 ;
    cout << "avg before type casting = " << avg << endl;

    avg = (float)(a + b + c) / 3 ;
    cout << "avg after type casting = " << avg << endl;

    return 0;
}