capypad
0 day streak
cpp / beginner
Snippet

Declaring Constants with const

The 'const' keyword creates a read-only variable. Once initialized, its value cannot be changed, which prevents accidental modifications.

snippet.cpp
cpp
1
2
const double PI = 3.14159;
// PI = 3.14; // This would cause a compiler error
Breakdown
1
const double PI = 3.14159;
Declares a constant double-precision floating-point number named PI.
2
// PI = 3.14;
Any attempt to reassign a value to a const variable results in an error.