Skip to content
Justice Tech School

Constants and Casts

const and constexpr declare constants, constexpr at compile time. Use static_cast for conversions and avoid old C style casts.

Example
constexpr double PI = 3.14159;
const int MAX = 10;
int a = 7, b = 2;
cout << a / b << " " << static_cast<double>(a) / b << "\n";
cout << PI * MAX << " " << static_cast<int>(3.99) << "\n";

Result

3 3.5
31.4159 3

Exercise

Safe conversion.

<double>(a)

Quiz Test

What does constexpr mean?

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