Type casting means converting a value from one data type to another. C++ provides several ways to perform type conversions, including implicit conversion and explicit casts.
Type casting is the process of converting a value from one data type to another data type.
double price = 99.50;
int value = static_cast<int>(price);
Here, the double value is explicitly converted to an int.
Type conversion is useful when different data types need to work together in an expression or when a specific type is required.
Implicit conversion happens automatically when C++ converts a value to another compatible type.
int number = 10;
double result = number;
The integer value is automatically converted to a double when it is assigned to result.
#include <iostream>
int main() {
int number = 10;
double value = number;
std::cout << value;
return 0;
}
The conversion from int to double happens automatically.
An integer can be converted to a floating-point type.
int marks = 85;
double result = marks;
The value 85 becomes a double value suitable for floating-point calculations.
A decimal value can be converted to an integer, but the fractional part is discarded.
double price = 99.75;
int value = static_cast<int>(price);
The value stored in value is 99.
Explicit conversion means that the programmer directly specifies the desired type.
double price = 99.75;
int value = static_cast<int>(price);
The static_cast expression clearly shows the intended conversion.
static_cast is the most common C++ cast for straightforward compile-time conversions between compatible types.
int number = 10;
double result = static_cast<double>(number);
Here, number is explicitly converted to double.
When two integers are divided, the result is integer division.
int a = 5;
int b = 2;
int result = a / b;
The result is 2, not 2.5.
You can convert one operand to a floating-point type before division.
int a = 5;
int b = 2;
double result = static_cast<double>(a) / b;
Now the result is 2.5.
It is usually enough to convert one operand because the other operand will then be converted as part of the arithmetic operation.
int a = 5;
int b = 2;
double result =
static_cast<double>(a) /
static_cast<double>(b);
The result is 2.5.
C-style casts can also be used in C++, although modern C++ generally prefers named casts because they make the intended conversion clearer.
double price = 99.75;
int value = (int)price;
For modern C++ code, static_cast<int>(price) is generally clearer.
The basic syntax of static_cast is:
static_cast<target_type>(value)
Example:
int number = 25;
double value = static_cast<double>(number);
A character can be converted to an integer type. The result represents the character's numeric code value in the execution character set.
char ch = 'A';
int code = static_cast<int>(ch);
std::cout << code;
The exact numeric code is determined by the character set used by the implementation.
An integer can also be explicitly converted to char.
int code = 65;
char ch = static_cast<char>(code);
std::cout << ch;
The displayed character depends on the execution character set.
A floating-point value can be converted to an integer.
float price = 45.89f;
int value = static_cast<int>(price);
The fractional part is discarded during this conversion.
An integer can be explicitly converted to a floating-point type.
int marks = 90;
float result = static_cast<float>(marks);
A double can be converted to a float.
double price = 99.987654;
float value = static_cast<float>(price);
This conversion may lose precision because float commonly has less precision than double.
A Boolean value can be converted to an integer.
bool passed = true;
int value = static_cast<int>(passed);
A true value converts to 1 and false converts to 0.
An integer can be converted to bool.
int number = 10;
bool result = static_cast<bool>(number);
Zero converts to false, while a non-zero value converts to true.
Type casting can be useful when a calculation requires a particular numeric type.
int totalMarks = 450;
int subjects = 5;
double average =
static_cast<double>(totalMarks) / subjects;
std::cout << average;
The result is calculated as a floating-point value.
A narrowing conversion can cause information to be lost. For example, converting a decimal value to an integer removes its fractional part.
double number = 25.75;
int result = static_cast<int>(number);
The value stored in result is 25.
#include <iostream>
int main() {
int marks = 85;
double percentage =
static_cast<double>(marks);
std::cout << percentage;
return 0;
}
The integer value is explicitly converted to a double value.
#include <iostream>
int main() {
int total;
int students;
std::cout << "Enter total marks: ";
std::cin >> total;
std::cout << "Enter number of students: ";
std::cin >> students;
double average =
static_cast<double>(total) / students;
std::cout << "Average: " << average;
return 0;
}
| Implicit Conversion | Explicit Conversion |
|---|---|
| Performed automatically | Requested directly by the programmer |
double x = 10; |
double x = static_cast<double>(10); |
| Less visible in source code | Clearly communicates the intended conversion |
#include <iostream>
int main() {
int obtained = 425;
int total = 500;
double percentage =
static_cast<double>(obtained) /
total * 100;
std::cout << "Percentage = "
<< percentage;
return 0;
}
Casting one operand to double ensures that the division is performed using floating-point arithmetic.
#include <iostream>
int main() {
int a = 10;
int b = 3;
int integerResult = a / b;
double decimalResult =
static_cast<double>(a) / b;
std::cout << "Integer Result: "
<< integerResult << std::endl;
std::cout << "Decimal Result: "
<< decimalResult << std::endl;
return 0;
}
The first division uses integer arithmetic, while the second explicitly converts one operand to double.
C++ provides several named cast operators for different situations:
For beginner-level numeric conversions, static_cast is the most commonly encountered named cast.
| Concept | Example |
|---|---|
| Implicit conversion | double x = 10; |
| Explicit conversion | double x = static_cast<double>(10); |
| Double to int | int x = static_cast<int>(10.5); |
| Int to double | double x = static_cast<double>(10); |
| Integer division | 5 / 2 gives 2 |
| Floating-point division | static_cast<double>(5) / 2 gives 2.5 |
Question: Which C++ cast is commonly used for straightforward explicit numeric conversions?