C++ Type Conversion
Type Conversion
Sometimes, you have to convert the value of one data type to another type. This is known as type conversion.
For example, if you try to divide two integers, 5 by 2, you might expect the result to be 2.5. But since we are working with integers (and not floating-point values), the following example will just output 2:
Example
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 2;
int result = x / y;
cout << result; // Outputs 2
return 0;
}
Try it Yourself »
To get the right result, you need to know how type conversion works.
There are two types of conversion in C++:
- Implicit Conversion (automatically)
- Explicit Conversion (manually)
Implicit Conversion
Implicit conversion is done automatically by the compiler when you mix types in an expression or assign a value of one type to another.
For example, assigning an int to a double:
Example
#include <iostream>
using namespace std;
int main() {
double myDouble = 9; // Automatic conversion: int to double
cout << myDouble; // 9
return 0;
}
Try it Yourself »
Be careful with implicit conversion in the other direction, which may truncate the value (the decimal part is lost):
Example
#include <iostream>
using namespace std;
int main() {
int myInt = 9.99; // Automatic conversion: double to int (truncates)
cout << myInt; // 9
return 0;
}
Try it Yourself »
As for integer division, even if the result is stored in a floating-point variable, dividing two integers still performs integer division first:
Example
#include <iostream>
using namespace std;
int main() {
double sum = 5 / 2; // both operands are int, so 5/2 == 2
cout << sum; // 2
return 0;
}
Try it Yourself »
To get a floating-point result, at least one operand must be floating-point.
Explicit Conversion
Explicit conversion is done manually. In C++ you should prefer the static_cast<> operator because it is clear and type-safe.
Example: make one operand double
#include <iostream>
using namespace std;
int main() {
double sum = static_cast<double>(5) / 2; // 5.0 / 2 == 2.5
cout << sum; // 2.5
return 0;
}
Try it Yourself »
You can also cast a variable:
Example: cast a variable
#include <iostream>
using namespace std;
int main() {
int num1 = 5;
int num2 = 2;
double sum = static_cast<double>(num1) / num2;
cout << sum; // 2.5
return 0;
}
Try it Yourself »
If you want to control the number of decimals in the output, use iostream formatting:
Example: control decimal precision
#include <iostream>
#include <iomanip> // for fixed and setprecision
using namespace std;
int main() {
int num1 = 5;
int num2 = 2;
double sum = static_cast<double>(num1) / num2;
cout << fixed << setprecision(1) << sum; // 2.5
return 0;
}
Try it Yourself »
C-Style Cast vs C++ Casts
You may also see the C-style cast syntax, for example (double)num1. It works, but modern C++ encourages using static_cast<> because it is clearer and helps the compiler find mistakes.
Example: C-style cast (works, but less explicit)
#include <iostream>
using namespace std;
int main() {
int a = 7, b = 4;
double q = (double)a / b; // 1.75
cout << q;
return 0;
}
Try it Yourself »
Tip: Prefer static_cast<> for ordinary numeric conversions. It documents your intent and is safer than a C-style cast.
Summary
- Implicit conversion happens automatically (for example, assigning an
intto adouble). - Integer division with two integers discards the fractional part. Make one operand floating-point to get a fractional result.
- Use
static_cast<>for explicit, clear, and safer conversions in C++.
Real-Life Example
Here’s a real-life example of type conversion where we create a program to calculate the percentage of a user's score in relation to the maximum score in a game:
Example
#include <iostream>
#include <iomanip> // for setprecision
using namespace std;
int main() {
// Set the maximum possible score in the game to 500
int maxScore = 500;
// The actual score of the user
int userScore = 423;
/*
Calculate the percentage of the user's score in relation to
the maximum available score.
Convert userScore to double to make sure the division is accurate.
*/
double percentage = static_cast<double>(userScore) / maxScore * 100.0;
// Print the percentage with 2 decimal places
cout << fixed << setprecision(2);
cout << "User's percentage is " << percentage << "%" << endl;
return 0;
}
Try it Yourself »