C++Constants

Constants in C++

In C++, constants are variables whose values cannot be changed after they are defined. They are useful when you want to ensure that specific values remain fixed throughout the execution of a program — such as mathematical constants, configuration values, or settings that should not be altered accidentally.

Using constants makes your code more readable, more maintainable, and less error-prone.


What is a Constant?

A constant is a value that, once assigned, cannot be modified. It is declared using the const keyword.

Syntax:

const <data_type> <constant_name> = <value>;

Example: Declaring a Constant

constant_example.cpp
#include <iostream>
 
int main() {
    const float PI = 3.14159;
    std::cout << "Value of PI: " << PI << std::endl;
    return 0;
}

In this example:

  • const makes the variable PI constant
  • The value 3.14159 cannot be changed once assigned

Attempting to Modify a Constant

Trying to change the value of a constant will result in a compilation error.

modify_constant.cpp
#include <iostream>
 
int main() {
    const int MAX = 100;
    MAX = 200; // Error: cannot assign to a variable declared 'const'
    return 0;
}

Why Use Constants?

ReasonDescription
Prevent accidental changesEnsures critical values stay unchanged
Improve readabilityMakes your code easier to understand and maintain
Self-documentingNames like MAX_USERS or PI are clearer than using raw numbers directly
Help with debuggingReduces the risk of silent logical errors

Types of Constants in C++

1. Literal Constants

Direct values used in code (e.g., 10, 'A', 3.14) — these are implicit constants.

2. const Variables

Declared using the const keyword.

const_variable.cpp
const int MIN_AGE = 18;

3. #define Macros (Preprocessor Constants)

Another way to define constants, mostly used in C and old-style C++.

define_macro.cpp
#define PI 3.14159

Use const instead of #define in modern C++ for type safety and better scoping.


Constant Expressions (constexpr)

Introduced in C++11, constexpr defines values evaluated at compile-time instead of run-time.

constexpr_example.cpp
constexpr int square(int x) {
    return x * x;
}

Use constexpr when you need the value during compilation, such as defining array sizes.


Constants with Different Data Types

const_types.cpp
const int MAX_SCORE = 100;
const char GRADE = 'A';
const float TEMPERATURE = 36.5;
const bool IS_ACTIVE = true;

Practice: Area of a Circle with const

circle_area_const.cpp
#include <iostream>
 
int main() {
    const float PI = 3.14;
    float radius = 7.0;
    float area = PI * radius * radius;
    std::cout << "Area: " << area << std::endl;
    return 0;
}

Practice: Yes or No?

Q1. Can a constant value be modified later in the program? No

Q2. Is #define type-safe like const? No

Q3. Can constants be used in expressions? Yes

Q4. Does const require initialization during declaration? Yes

Q5. Is constexpr evaluated at compile time? Yes


Summary

FeatureDescription
constDeclares a variable as constant
#definePreprocessor constant (avoid in modern C++)
constexprCompile-time constant (C++11 and newer)
Must initializeconst variables must be assigned at declaration
Cannot modifyAttempting to change a constant results in compile error
Improves safetyPrevents unintentional value changes

Tip for Beginners

Use const whenever you want to make your intent explicit and protect values from being modified. It’s a simple but powerful tool for writing reliable code.