Tsallaka zuwa abun ciki
Justice Tech School

Casting

Use static_cast for safe conversions. Avoid C style casts.

Example
#include <iostream>

int main() {
  double d = 9.99;
  int n = static_cast<int>(d);
  char c = static_cast<char>(67);
  std::cout << n << " " << c << " " << static_cast<double>(7) / 2 << "\n";
  return 0;
}

Result

9 C 3.5

Exercise

Safe cast.

<int>(d)

Quiz Test

Why prefer static_cast?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.