Type casting helps you change a value from one type to another. It is useful when you want to treat data differently or avoid errors.
Type casting in C++
Introduction
Syntax
C++
type new_variable = (type) old_variable; // C-style cast new_variable = static_cast<type>(old_variable); // C++ style cast
C++ prefers static_cast because it is safer and clearer.
C-style casts can do many conversions but are less safe and harder to find in code.
Examples
a to double using C-style cast.C++
int a = 10; double b = (double) a;
x to int using static_cast, dropping decimals.C++
double x = 9.7; int y = static_cast<int>(x);
c to its ASCII integer value.C++
char c = 'A'; int ascii = static_cast<int>(c);
Sample Program
This program shows how to convert a double to an int and a char to its ASCII integer value using static_cast.
C++
#include <iostream> int main() { double pi = 3.14159; int intPi = static_cast<int>(pi); // convert double to int std::cout << "Original double: " << pi << "\n"; std::cout << "After casting to int: " << intPi << "\n"; char letter = 'Z'; int asciiValue = static_cast<int>(letter); // char to int std::cout << "Character: " << letter << " has ASCII value: " << asciiValue << "\n"; return 0; }
Important Notes
When casting from floating-point to integer, the decimal part is dropped, not rounded.
Use static_cast for most conversions to keep code clear and safe.
Be careful when casting pointers; improper casts can cause crashes.
Summary
Type casting changes a value from one type to another.
Use static_cast in C++ for safe and clear conversions.
Casting helps avoid errors and makes your code intentions clear.
Practice
1. What does
static_cast<int>(3.14) do in C++?easy
Solution
Step 1: Understand static_cast usage
static_cast<int>converts a value to an integer type explicitly.Step 2: Apply static_cast to 3.14
The floating-point number 3.14 is converted to integer by dropping the decimal part, resulting in 3.Final Answer:
Converts the floating-point number 3.14 to the integer 3 -> Option DQuick Check:
static_cast<int>(3.14) = 3 [OK]
Hint: static_cast truncates decimals when converting float to int [OK]
Common Mistakes:
- Thinking it rounds the number instead of truncating
- Confusing static_cast with dynamic_cast
- Expecting a runtime error from static_cast
2. Which of the following is the correct syntax for casting a
double variable d to int using static_cast?easy
Solution
Step 1: Recall static_cast syntax
The correct syntax isstatic_cast<type>(expression).Step 2: Match syntax with options
int i = static_cast<int>(d); matches the correct syntax:int i = static_cast<int>(d);.Final Answer:
int i = static_cast<int>(d); -> Option BQuick Check:
Correct static_cast syntax = int i = static_cast<int>(d); [OK]
Hint: Use angle brackets for type in static_cast [OK]
Common Mistakes:
- Using parentheses instead of angle brackets for type
- Placing type after variable instead of inside <>
- Mixing C-style cast syntax with static_cast
3. What is the output of the following C++ code?
double x = 9.99; int y = static_cast<int>(x); std::cout << y << std::endl;
medium
Solution
Step 1: Understand static_cast behavior on double to int
static_cast truncates the decimal part, so 9.99 becomes 9.Step 2: Predict output of std::cout
The integer 9 is printed to the console.Final Answer:
9 -> Option CQuick Check:
static_cast<int>(9.99) = 9 [OK]
Hint: Casting double to int drops decimals, no rounding [OK]
Common Mistakes:
- Expecting rounding to 10 instead of truncation
- Confusing output type with original double
- Thinking static_cast causes compile error here
4. Identify the error in this code snippet:
float f = 3.5f; int i = static_cast<int> f; std::cout << i;
medium
Solution
Step 1: Check static_cast syntax
static_cast requires parentheses around the value to cast, likestatic_cast<int>(f).Step 2: Identify missing parentheses
The code misses parentheses afterstatic_cast<int>, causing syntax error.Final Answer:
Missing parentheses after static_cast<int> -> Option AQuick Check:
static_cast<int>(f) needs parentheses [OK]
Hint: Always use parentheses after static_cast type [OK]
Common Mistakes:
- Omitting parentheses after static_cast
- Thinking static_cast can't convert float to int
- Ignoring compiler error messages
5. Given the code below, what will be the output?
Note: ASCII value 65 corresponds to 'A'.
int a = 65; char c = static_cast<char>(a); std::cout << c << std::endl;
Note: ASCII value 65 corresponds to 'A'.
hard
Solution
Step 1: Understand static_cast from int to char
static_cast converts the integer 65 to its corresponding ASCII character 'A'.Step 2: Predict output of std::cout
Printing the char variable outputs the character 'A'.Final Answer:
'A' -> Option AQuick Check:
static_cast<char>(65) = 'A' [OK]
Hint: Casting int to char prints ASCII character [OK]
Common Mistakes:
- Expecting numeric 65 to print instead of 'A'
- Thinking casting int to char causes error
- Confusing char with string output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
