100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Constants in C++

Learn what constants are in C++, the different ways to define them (const, #define, constexpr), and when to use each.

BasicsBeginner6 min readJul 7, 2026
Analogies

1. Intro

A constant is a value that cannot be changed once it is set. Unlike a variable, whose value can be updated anytime during execution, a constant is fixed for the lifetime of the program — useful for values like tax rates, mathematical constants, or configuration limits that must never accidentally change.

🏏

Cricket analogy: A constant is like the fixed boundary rope distance of 70 yards set before a match — unlike a batsman's ever-changing score, it never changes once the ground is marked, just like PI never changes in your code.

2. Syntax

C++ gives you three main ways to define a constant:

🏏

Cricket analogy: Just as a captain has three tools to enforce a fixed field restriction — the rulebook, the umpire's call, and the pitch markings — C++ gives you three distinct ways to lock in a constant value.

  • const keyword — const double PI = 3.14159;
  • Preprocessor macro — #define PI 3.14159
  • constexpr (C++11 and later) — constexpr double PI = 3.14159;

3. Explanation

The const keyword tells the compiler that a variable's value must not change after initialization — any later assignment (PI = 3.0;) is a compile-time error. const is type-checked and scoped like a normal variable, which makes it the preferred, safer choice in modern C++.

🏏

Cricket analogy: The const keyword is like an ICC rule locking the boundary size once a Test match starts — trying to move the rope mid-match (PI = 3.0;) gets flagged immediately by the match referee, just like a compile-time error.

#define is a preprocessor directive, not a real variable — it performs a simple text substitution before compilation even starts, so the compiler never sees PI, only 3.14159. Because it bypasses the type system, it is generally considered outdated for defining numeric constants in modern C++, though it is still common in legacy code.

🏏

Cricket analogy: #define is like a scorer using find-and-replace to swap every mention of 'Team A' with 'Mumbai Indians' before printing the scorecard — the printed sheet only ever shows 'Mumbai Indians', bypassing any check on whether that's really a valid team name, a technique still seen in older scorebooks.

constexpr, introduced in C++11, goes a step further than const: it tells the compiler the value must be computable at compile time, which allows it to be used in contexts requiring compile-time constants (like array sizes) and can enable extra compiler optimizations.

🏏

Cricket analogy: constexpr is like a fixture computed and locked in before the tournament schedule is even printed — because it's known at scheduling time, it can be used to fix the exact number of group-stage matches, unlike a live score that's only known during play.

A constant must always be initialized at the point of declaration — const int MAX; without a value is a compile-time error, since there would be no way to ever assign it afterward.

4. Example

cpp
#include <iostream>
using namespace std;

#define CM_PER_INCH 2.54
const double PI = 3.14159;

int main() {
    constexpr int MAX_USERS = 100;

    cout << "PI: " << PI << endl;
    cout << "1 inch in cm: " << CM_PER_INCH << endl;
    cout << "Max users: " << MAX_USERS << endl;

    // PI = 3.0;  // Error: assignment of read-only variable

    return 0;
}

5. Output

text
PI: 3.14159
1 inch in cm: 2.54
Max users: 100

6. Key Takeaways

  • A constant holds a value that cannot change after initialization.
  • const creates a type-checked, scoped constant — the preferred modern approach.
  • #define performs raw text substitution before compilation and is not type-checked.
  • constexpr guarantees a compile-time-computable constant, enabling extra optimizations and compile-time use cases.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ConstantsInC#Constants#Syntax#Explanation#Example#StudyNotes#SkillVeris#ExamPrep