Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Constants & Type Casting · Lesson 25 of 25

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

🤖
Converting between types isn't always free — some directions are riskier. 🔄

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
This implicit conversion happens automatically because every int value fits perfectly inside a double.
🤖
static_cast is C++'s way of making you own the risk. ✍️

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
Casting a double to int truncates (cuts off) the decimal part — it does not round.
🤖
You may also see this shorter style in older code. 👀

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++
Both lines produce 9, but static_cast is the style this course recommends going forward.
Fill in the blank

The modern, recommended way to explicitly convert a double to an int in C++ is ____.

🤖
Overflow bugs are sneaky — the number doesn't error, it just lies. 🐛

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
Casting a value too large to fit into int causes it to overflow instead of simply being cut down.
Quick check

What does static_cast<int>(9.99) produce?

True or false

Converting an int to a double happens implicitly in C++, without needing static_cast.

Chapter quiz

Constants & Type Casting

10 questions · pass with 60% — just like in the app.

Fill in the blank

The ____ keyword tells C++ that a variable's value can never change after it is initialized.

Quick check

What happens if you try to reassign a variable declared with const?

True or false

A const variable must be given its value at the moment it is declared.

Fill in the blank

By convention, constants are often named in ____, such as MAX_PLAYERS.

Quick check

Why are named constants preferred over unexplained "magic numbers" scattered through code?

True or false

Converting an int to a double happens implicitly in C++, with no cast needed.

Fill in the blank

The modern, recommended way to explicitly convert a double to an int is ____.

Quick check

What does static_cast<int>(9.99) produce?

True or false

The older C-style cast (int) price and static_cast<int>(price) both work, but static_cast is the preferred modern style.

Quick check

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 →