Type Casting in C++

Level 281 keystrokes

Converts the double 3.14159 to an int with static_cast; the fractional part is dropped, so 3 is printed.

C++ code

// Stores an approximation of pi in a double (a floating-point number)
1double pi = 3.14159;
// static_cast<int> converts the value to int explicitly
// The conversion truncates toward zero instead of rounding: 3.14159 becomes 3
2int rounded = static_cast<int>(pi);
// Prints the converted value, 3
3cout << rounded << endl;

Output

3
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 5
  • ;× 3
  • =× 2
  • _× 1
  • .× 1
  • (× 1
  • )× 1
  • >× 1

More C++ examples at level 2

← C++ typing practice