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
#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
PI: 3.14159
1 inch in cm: 2.54
Max users: 1006. Key Takeaways
- A constant holds a value that cannot change after initialization.
constcreates a type-checked, scoped constant — the preferred modern approach.#defineperforms raw text substitution before compilation and is not type-checked.constexprguarantees a compile-time-computable constant, enabling extra optimizations and compile-time use cases.
Practice what you learned
1. What is a constant in C++?
2. Which keyword defines a type-checked constant in C++?
3. What does `#define PI 3.14` actually do?
4. What is required when declaring a `const` variable?
5. What advantage does `constexpr` have over a plain `const`?
Was this page helpful?
You May Also Like
Variables in C++
Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.
Literals in C++
Learn what literals are in C++ — integer, floating-point, character, string, and boolean literals — with syntax and examples.
Data Types in C++
Learn the fundamental data types in C++ — int, float, double, char, bool, and void — with sizes, ranges, and examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics