Type Conversions
& Casts in C++
Learn how C++ converts between types implicitly and explicitly, why narrowing conversions are dangerous, and how to use casts safely.
What Is a Type?
Every variable in C++ has a type that tells the compiler two things: how much memory to allocate and how to interpret the bits stored there. Different types hold different ranges of values.
Implicit vs Explicit Conversions
C++ performs type conversions in two ways. Understanding the difference is the foundation of writing safe, predictable code.
The Promotion Ladder
When you mix types in an expression, C++ promotes the narrower type to the wider type. Data flows upward safely:
The Core Rule
Visualizing Narrowing
Widening vs Narrowing
| Widening | Narrowing | |
|---|---|---|
| Direction | Small type → Large type | Large type → Small type |
| Data loss? | No | Possible |
| Example | int → double |
double → int |
| Implicit OK? | Yes, always safe | Risky: use explicit cast |
| Brace init {} | Allowed | Compile error |
C++ Cast Operators
C++ provides four named cast operators. For introductory code, you will primarily use static_cast. The old C-style cast (int)x still works but is discouraged.
Using static_cast
Step-by-Step Checklist
static_cast<T>() to make it explicit. This prevents compiler warnings and signals intent.int x{val} to catch accidental narrowing at compile time.Example 1: Integer Division Fix
Problem: dividing two integers gives an integer result, even when you want a decimal answer.
Example 2: Extracting a Character Code
Scenario: you want to see the numeric ASCII value of a character, or convert a number back to a character.
Example 3: Brace Init Protection
C++11 brace initialization catches narrowing conversions that old-style = lets through silently.
Common Mistakes
(int)x instead of static_cast<int>(x). C-style casts bypass safety checks and are harder to search for in code.7/2 and expecting \(3.5\). Both operands are int, so the result is \(3\). Cast one operand to double first.Summary Table
| Conversion | Type | Safe? | Action |
|---|---|---|---|
int → double |
Widening | Yes | Implicit OK |
double → int |
Narrowing | No | static_cast |
int → char |
Narrowing | No | static_cast |
char → int |
Widening | Yes | Implicit OK |
int → unsigned |
Narrowing (sign) | Risky | static_cast + validate |
float → double |
Widening | Yes | Implicit OK |
Question 1 of 5
What is the value of x after this code runs?double d = 9.78; int x = d;
double to an int truncates the decimal: \(9.78\) becomes \(9\) (not rounded). This is a narrowing conversion that compiles silently with = init.Question 2 of 5
Which line will cause a compiler error due to narrowing?
{} forbids narrowing conversions. int y{3.14} is a narrowing conversion from double to int and causes a compile error. Option D uses = which allows it silently.Question 3 of 5
What does static_cast<double>(7) / 2 evaluate to?
double makes it \(7.0\). The compiler then promotes \(2\) to \(2.0\), and \(7.0 / 2.0 = 3.5\). Without the cast, 7 / 2 would give \(3\) (integer division).Question 4 of 5
Which cast operator should you use for standard type conversions in everyday C++ code?
static_cast is the standard cast for converting between related types (int to double, etc.). dynamic_cast is for class hierarchies, reinterpret_cast is for low-level bit reinterpretation, and const_cast is for adding/removing const.Question 5 of 5
Assigning char c = 'A'; followed by int n = c; is an example of:
char → int is a widening conversion. A char (1 byte) fits entirely within an int (4 bytes) with no data loss. The value stored is the ASCII code of 'A', which is \(65\).