Type Casting Part 1
Part of the Fundamentals section of Coddy's C++ journey — lesson 14 of 74.
Type casting is the process of converting a value from one data type to another.
In C++, we can convert integers to doubles, doubles to integers, and more. There are two types of casting: implicit (automatic) and explicit (manual) casting.
For example Integer to Double:
Implicit (automatic) casting:
int number = 5;
double decimal = number; // automatically becomes 5.0
// with calculation
int x = 7;
double result = x / 2.0; // result is 3.5Note: When dividing two int values, C++ performs integer division — the decimal part is discarded. For example, 7 / 2 gives 3, not 3.5. To get a decimal result, at least one operand must be a double (e.g., 7 / 2.0 gives 3.5).
Explicit (manual) Casting Double to Integer:
double decimal = 9.7;
int number = (int) decimal; // becomes 9 (decimal part is truncated)
// with calculation
double price = 19.99;
int roundedPrice = (int) price; // becomes 19Modern C++ preferred style: Instead of the C-style cast (int) decimal, it is better practice to use static_cast<>(), which is safer and more explicit about your intent:
double decimal = 9.7;
int number = static_cast<int>(decimal); // becomes 9 (decimal part is truncated)
double price = 19.99;
int roundedPrice = static_cast<int>(price); // becomes 19Note: Both (int) value and static_cast<int>(value) produce the same result here, but static_cast<>() is the recommended approach in modern C++ as it makes the conversion clearly visible and is checked by the compiler.
Challenge
BeginnerWrite a C++ program that demonstrates type casting. Perform the following:
Cast the price variable to an int and store the result in a new variable named intPrice.
Cheat sheet
Type casting converts values from one data type to another.
Implicit (automatic) casting — happens automatically:
int number = 5;
double decimal = number; // automatically becomes 5.0
int x = 7;
double result = x / 2.0; // result is 3.5 (int/int discards decimal)Explicit (manual) casting — C-style and modern static_cast:
double price = 19.99;
int a = (int) price; // C-style: becomes 19
int b = static_cast<int>(price); // modern C++ preferred: becomes 19Note: Casting a double to int truncates (drops) the decimal part. static_cast<>() is preferred in modern C++ for clarity and compiler safety.
Try it yourself
#include <iostream>
int main() {
// Declare and initialize variables
double price = 99.99;
int intPrice = ?; // Explicit casting from double to int
// Output the values
std::cout << "Price: " << price << std::endl;
std::cout << "Int Price: " << intPrice << std::endl;
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison3Variables Part 2
Type DeclarationNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementConditional OperatorRecap - If ElseNested If - Else