Type Casting
A free lesson from Swipe Coding · C++, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Type casting means converting a value from one type to another. C++ has two kinds: implicit casting, which happens automatically when no data is likely to be lost, and explicit casting, which you write yourself because it risks losing information.
int wholeNumber = 10; double decimalNumber = wholeNumber; // implicit casting std::cout << decimalNumber << std::endl; // 10
Going the other way — from a larger or more precise type to a smaller one, like double to int — risks losing information, so it is good practice to make the conversion explicit using static_cast<targetType>(value). This tells anyone reading the code exactly what conversion is intended.
double price = 9.99; int wholePrice = static_cast<int>(price); std::cout << wholePrice << std::endl; // 9
You might also come across an older, C-style cast that looks like (int) price instead of static_cast<int>(price). Both work, but static_cast is the modern, recommended style in C++: it stands out clearly in the code and the compiler can check it more strictly, catching more mistakes at compile time.
double price = 9.99; int oldStyle = (int) price; // works, but not preferred int modernStyle = static_cast<int>(price); // preferred in modern C++
The modern, recommended way to explicitly convert a double to an int in C++ is ____.
Precision loss isn't limited to decimals: casting a whole-number type into a smaller one can also lose data if the value doesn't fit. When that happens, the number can "wrap around" into a completely unexpected — even negative — result, which is a common source of subtle bugs.
long long big = 3000000000; int smaller = static_cast<int>(big); std::cout << smaller << std::endl; // overflows to an unexpected, incorrect number
What does static_cast<int>(9.99) produce?
Converting an int to a double happens implicitly in C++, without needing static_cast.
Constants & Type Casting
10 questions · pass with 60% — just like in the app.
The ____ keyword tells C++ that a variable's value can never change after it is initialized.
What happens if you try to reassign a variable declared with const?
A const variable must be given its value at the moment it is declared.
By convention, constants are often named in ____, such as MAX_PLAYERS.
Why are named constants preferred over unexplained "magic numbers" scattered through code?
Converting an int to a double happens implicitly in C++, with no cast needed.
The modern, recommended way to explicitly convert a double to an int is ____.
What does static_cast<int>(9.99) produce?
The older C-style cast (int) price and static_cast<int>(price) both work, but static_cast is the preferred modern style.
What can happen when you cast a whole number into a smaller type that cannot hold it?
Keep your streak going in Swipe Coding
Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →