Constants in C++ are values that remain unchanged throughout the execution of a program. They help improve code reliability, readability, and maintainability by preventing unintended modifications.
- Can be defined using const, constexpr, or #define.
- Used to represent fixed values such as limits, configuration settings, and mathematical constants.
- Help make code easier to understand and maintain.
#include <iostream>
using namespace std;
int main() {
// Declaring and defining a constant variable
const int c = 24;
cout << c;
return 0;
}
Output
24
Explanation: The variable c is declared as a constant using the const keyword. Any attempt to modify its value later in the program will result in a compilation error.
Types of Constants in C++
In C++, constants can be classified based on how they are created. They can be defined using the following three ways:
1. Constants Using const
The const keyword is used to declare a variable whose value cannot be changed after initialization.
Syntax
const DATATYPE variable_name =value;
#include <iostream>
using namespace std;
int main() {
int var = 10;
// Declaring a constant variable
const int c = 24;
// Trying to change the value constant c
c = 0;
cout << c;
return 0;
}
Output
./Solution.cpp: In function 'int main()':
./Solution.cpp:19:10: error: assignment of read-only variable 'c'
cons = 0;
Explanation: The variable c is initialized with the value 24. Since it is declared as const, attempting to modify it later results in a compilation error.
2. Constants Using constexpr
The constexpr keyword is used to declare compile-time constants in C++. Unlike const, a constexpr variable must be initialized with a value that is known at compile time.
Syntax
constexpr DATATYPE variable_name = value ;
#include <iostream>
using namespace std;
int main() {
// Defining constant
int constexpr hoursIn_day = 24;
// Printing value
cout << hoursIn_day;
return 0;
}
Output
24
Explanation hoursInDay is evaluated at compile time, allowing the compiler to perform additional optimizations.
3. Constants Using #define
The #define directive creates macro constants by replacing names with values during preprocessing. It is less preferred because it does not provide type safety.
Syntax
#define MACRO_NAME replacement_value
#include <iostream>
using namespace std;
// Using #define to create a macro
#define Side 5
int main()
{
// Using constant
double area = Side * Side;
cout << area;
return 0;
}
Output
25
Explanation: The preprocessor replaces every occurrence of Side with 5 before compilation begins.
Note: Macro constants are generally less preferred than const or constexpr because they do not provide type safety.
Real-World Examples of Constants
ATM Daily Withdrawal Limit: In banking software, a customer may have a fixed limit of ₹20,000 per day.
const int dailyLimit = 20000;
Max Login Attempts: Apps like WhatsApp or banking apps usually allow only 3 login attempts.
const int maxLoginAttempts = 3;
Value of Pi (π): The value of π is always the same in mathematical formulas.
const float pi = 3.14159;
Advantages of Constants
Using constants provides several benefits:
- Prevents accidental modification of values.
- Improves code readability and maintainability.
- Makes programs easier to update by avoiding hard-coded values.
- Enables compiler optimizations in some cases.
Limitations of Constants
Although useful, constants have some restrictions:
- A const variable cannot be modified after initialization.
- Constants must usually be initialized when declared.
- Incorrect use of constants with pointers can be confusing for beginners.
Note: Constants and literals are different concepts in C++. For a detailed comparison, see Constants vs Literals in C++