C++20 sections · 1024 units
Open in Course

Type Conversion - Explicit

Forced changes

You can force a type conversion with a cast: int(3.7) or static_cast<int>(3.7) both give you 3. This makes your intent clear to readers. Use casting when you want integer division to return a decimal: double result = double(7) / 2; gives you 3.5 instead of 3.

Casting does not change the original variable. double x = 3.7; int y = int(x); leaves x as 3.7 and stores 3 in y as a separate value.