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.
- Can occur automatically (implicit conversion) or manually (explicit conversion).
- Helps ensure compatibility between different data types. C++ `
#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:
- Performing operations on values of different data types.
- Passing arguments to functions expecting a different type.
- Assigning a value of one type to a variable of another compatible type.

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
- **Numeric Conversions: When arithmetic operations involve different numeric types, the compiler converts operands according to the usual arithmetic conversion rules.
- **Pointer Conversions: Pointers to derived classes can be implicitly converted to pointers to their base classes.
- **Boolean Conversions: Integers, floating-point values, and pointers can be implicitly converted to
boolwhen used in conditional expressions.
**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 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:
- type is the target data type.
- expression is the value being converted. C++ `
#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
- static_cast – Performs standard compile-time type conversions.
- dynamic_cast – Performs runtime conversions in polymorphic class hierarchies.
- const_cast – Adds or removes const and volatile qualifiers.
- reinterpret_cast – Performs low-level reinterpretation of memory or pointer types. C++ `
#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:
- Data loss when converting from a larger type to a smaller type.
- Precision loss when converting floating-point values to integers.
- Overflow when a value exceeds the range of the destination type.
- Undefined behavior when casting between unrelated pointer types.
- Violating const-correctness by modifying an object after removing const.