Type Conversion in C++ (original) (raw)

Last Updated : 13 Jun, 2026

Type conversion is the process of converting a value from one data type to another compatible data type. It allows different types of data to work together in expressions, assignments, and function calls.

#include using namespace std;

int main() {

  // Two variables of different type
int i = 10;
char c = 'A';

  // printing c after manually converting it
  cout << (int)c << endl;
  
  // Adding i and c,
  int sum = i + c;

  // Printing sum
  cout << sum;
  
return 0;

}

`

**Explanation: In the example, (int)c explicitly converts the character 'A' to its ASCII value 65. During the addition i + c, the character is implicitly converted to an integer before the operation is performed.

Types of Type Conversion

C++ supports two types of type conversion:

Implicit Type Conversion

Implicit type conversion (also called coercion) is performed automatically by the compiler whenever a value needs to be converted to a compatible type. It commonly occurs when:

widening_type_casting

C++ Widening Type Hierarchy

C++ `

#include using namespace std;

int main() {

int i = 10;
char c = 'a';

// c implicitly converted to int. ASCII
// value of 'a' is 97
i = i + c;

// x is implicitly converted to float
float f = i + 1.0;

cout << "i = " << i << endl
     << "c = " << c << endl
     << "f = " << f;

return 0;

}

`

Output

i = 107 c = a f = 108

**Explanation: In the example, the character 'a' is automatically converted to its ASCII value (97) before being added to the integer variable.

Cases of Implicit Type Conversion

**Note: Implicit conversions may cause data loss, sign changes, or overflow when converting between incompatible types.

Explicit Type Conversion

Explicit type conversion, also known as type casting, is performed manually by the programmer to convert a value from one type to another.

explicit_type_casting_order

Explicit Narrowing Conversion Order

In C++, it can be done by two ways:

**1. C Style Typecasting

This method is inherited by C++ from C. The conversion is done by explicitly defining the required type in front of the expression in parenthesis. This can be also known as forceful casting.

(type) expression;

**where:

#include using namespace std;

int main() { double x = 1.2;

// Explicit conversion from double to int
int sum = (int)x + 1;

cout << sum;

return 0;

}

`

**Explanation: The value of x is explicitly converted from double to int before being used in the expression.

**Note: C-style casts are generally less safe because they do not clearly indicate the kind of conversion being performed.

**2. C++ Style Typecasting

C++ provides dedicated cast operators that make conversions safer and more explicit.

**Types of C++ Cast Operators

#include using namespace std;

int main() { double x = 1.2;

// Explicit conversion from double to int
int sum = static_cast<int>(x + 1);

cout << sum;
return 0;

}

`

**Explanation: static_cast(x) explicitly converts the value of x to an integer before it is used.

Risks of Type Conversion

Although type conversion is useful, improper conversions can introduce errors: