Bird
Raised Fist0
C++programming~10 mins

Type casting in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Type casting
Start with a value
↓
Choose target type
↓
Apply cast syntax
↓
Convert value to target type
↓
Use converted value
↓
End
Type casting changes a value from one type to another using explicit syntax, allowing different operations or storage.
Execution Sample
C++
int main() {
  double pi = 3.14;
  int intPi = (int)pi;
  return intPi;
}
This code converts a double value 3.14 to an int by cutting off the decimal part.
Execution Table
StepVariableOriginal ValueCast TypeResult after CastExplanation
1pi3.14 (double)-3.14 (double)Initial double value assigned
2intPi3.14 (double)int3 (int)Cast double to int truncates decimal
3returnintPi-3Return the integer value
4----Program ends
💡 Program ends after returning the casted integer value 3
Variable Tracker
VariableStartAfter CastFinal
pi3.14 (double)3.14 (double)3.14 (double)
intPiuninitialized3 (int)3 (int)
Key Moments - 3 Insights
Why does casting from double to int remove the decimal part?
Casting from double to int truncates the decimal part instead of rounding, as shown in step 2 of the execution_table where 3.14 becomes 3.
Is the original variable 'pi' changed after casting?
No, 'pi' remains a double with value 3.14. The cast creates a new int value in 'intPi' as shown in variable_tracker.
What happens if you cast an int to a double?
Casting int to double converts the integer to a floating-point number with decimal zero, e.g., 3 becomes 3.0, but this example shows double to int.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'intPi' after casting?
A3.14
B3
C0
D4
💡 Hint
Check step 2 in the execution_table where 'intPi' is assigned after casting.
At which step does the decimal part get removed?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Explanation' column in execution_table for when casting happens.
If we changed the cast to (float) instead of (int), what would 'intPi' hold?
A0
B3.14 (float)
C3 (int)
DError
💡 Hint
intPi is an int variable, so even casting to float first, it truncates the decimal to 3.
Concept Snapshot
Type casting in C++ changes a value's type explicitly.
Syntax: (target_type) value or static_cast<target_type>(value).
Casting double to int truncates decimals.
Original variable stays unchanged.
Useful to convert types for operations or storage.
Full Transcript
This example shows type casting in C++. We start with a double variable pi holding 3.14. We cast it to int using (int)pi, which removes the decimal part and stores 3 in intPi. The original pi remains 3.14. The program returns intPi which is 3. This shows how casting changes the type and value representation explicitly.

Practice

(1/5)
1. What does static_cast<int>(3.14) do in C++?
easy
A. Leaves the value unchanged as 3.14
B. Converts the integer 3 to the floating-point number 3.14
C. Causes a runtime error
D. Converts the floating-point number 3.14 to the integer 3

Solution

  1. Step 1: Understand static_cast usage

    static_cast<int> converts a value to an integer type explicitly.
  2. 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.
  3. Final Answer:

    Converts the floating-point number 3.14 to the integer 3 -> Option D
  4. Quick 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
A. int i = (int)static_cast(d);
B. int i = static_cast<int>(d);
C. int i = static_cast(d, int);
D. int i = static_cast<int>d;

Solution

  1. Step 1: Recall static_cast syntax

    The correct syntax is static_cast<type>(expression).
  2. Step 2: Match syntax with options

    int i = static_cast<int>(d); matches the correct syntax: int i = static_cast<int>(d);.
  3. Final Answer:

    int i = static_cast<int>(d); -> Option B
  4. Quick 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
A. 9.99
B. 10
C. 9
D. Compilation error

Solution

  1. Step 1: Understand static_cast behavior on double to int

    static_cast truncates the decimal part, so 9.99 becomes 9.
  2. Step 2: Predict output of std::cout

    The integer 9 is printed to the console.
  3. Final Answer:

    9 -> Option C
  4. Quick 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
A. Missing parentheses after static_cast<int>
B. Cannot cast float to int using static_cast
C. Variable i is not declared
D. Missing semicolon after float declaration

Solution

  1. Step 1: Check static_cast syntax

    static_cast requires parentheses around the value to cast, like static_cast<int>(f).
  2. Step 2: Identify missing parentheses

    The code misses parentheses after static_cast<int>, causing syntax error.
  3. Final Answer:

    Missing parentheses after static_cast<int> -> Option A
  4. Quick 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?
int a = 65;
char c = static_cast<char>(a);
std::cout << c << std::endl;

Note: ASCII value 65 corresponds to 'A'.
hard
A. 'A'
B. Compilation error
C. 65
D. Undefined behavior

Solution

  1. Step 1: Understand static_cast from int to char

    static_cast converts the integer 65 to its corresponding ASCII character 'A'.
  2. Step 2: Predict output of std::cout

    Printing the char variable outputs the character 'A'.
  3. Final Answer:

    'A' -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard