A constant is a value that is not intended to change during the execution of a program. In C++, the const keyword is commonly used to create objects whose values cannot be modified after initialization.
A constant is a value that should remain unchanged after it has been initialized.
const int age = 20;
Here, age is a constant integer variable. Its value cannot be changed after initialization.
Constants are useful when a value should remain fixed throughout a program or a particular scope.
The const keyword tells the compiler that an object cannot be modified through that object after initialization.
const int DAYS = 7;
The value of DAYS cannot be changed later.
The general syntax for declaring a const variable is:
const data_type variable_name = value;
Example:
const int MAX = 100;
A const object normally needs an initializer when it is declared.
const int number = 10;
Here, the value 10 is assigned at initialization.
After initialization, a const variable cannot be assigned a new value.
const int age = 20;
// age = 25; // Error
The assignment is not allowed because age is const.
An integer can be declared as a constant using const.
const int DAYS_IN_WEEK = 7;
The value remains 7.
Floating-point values can also be declared as constants.
const double PI = 3.14159;
This is useful for mathematical values that should remain fixed.
A character can also be declared as a constant.
const char GRADE = 'A';
The character stored in GRADE cannot be changed.
A Boolean value can also be made constant.
const bool PASSED = true;
The value remains true.
A std::string object can also be declared as const.
#include <string>
const std::string COLLEGE = "Soopro Pathshala";
The string object cannot be assigned a different string after initialization.
A constant can be displayed using std::cout, just like an ordinary variable.
#include <iostream>
int main() {
const int DAYS = 7;
std::cout << DAYS;
return 0;
}
Constants can be used in mathematical calculations.
const int PRICE = 100;
int quantity = 5;
int total = PRICE * quantity;
The constant PRICE is used to calculate the total amount.
Constants can be used inside expressions with other variables.
const double TAX = 0.18;
double price = 1000;
double taxAmount = price * TAX;
A program can contain many constants.
const int DAYS = 7;
const int MONTHS = 12;
const double PI = 3.14159;
Each constant can represent a different fixed value.
A const variable cannot be used for ordinary user input when its value needs to be changed after initialization.
const int age = 20;
// std::cin >> age; // Error
User input normally requires a non-const variable.
A normal variable can be assigned a new value. A const variable cannot.
int age = 20;
age = 25; // Allowed
const int year = 2026;
// year = 2027; // Error
Constant names can follow the same identifier rules as other C++ variables.
A common convention is to use uppercase letters with underscores:
const int MAX_SIZE = 100;
const double TAX_RATE = 0.18;
Uppercase naming is a convention, not a requirement of the language.
C++ is case-sensitive, so these are different identifiers:
const int MAX = 100;
const int Max = 200;
const int max = 300;
It is better to use a consistent naming convention.
A constant declared inside a function has local scope.
#include <iostream>
int main() {
const int DAYS = 7;
std::cout << DAYS;
return 0;
}
The constant DAYS is available within its scope.
A const variable can also be declared at namespace scope.
#include <iostream>
const int MAX_SCORE = 100;
int main() {
std::cout << MAX_SCORE;
return 0;
}
Such a constant can be used where its scope makes it visible.
The const keyword can be combined with auto. The compiler deduces the type from the initializer.
const auto age = 20;
const auto price = 99.50;
The resulting objects are const and cannot be modified.
#include <iostream>
int main() {
const double PI = 3.14159;
double radius = 5;
double area = PI * radius * radius;
std::cout << "Area = " << area;
return 0;
}
The value of PI remains fixed while the radius can be changed.
| Normal Variable | Constant |
|---|---|
| Can normally be changed | Cannot normally be changed after initialization |
Example: int age = 20; |
Example: const int AGE = 20; |
| Can receive later assignments | Cannot receive a new value after initialization |
Using a meaningful constant name can make code easier to understand.
Instead of:
double total = price * 0.18;
You can write:
const double TAX_RATE = 0.18;
double total = price * TAX_RATE;
The name TAX_RATE clearly explains what the value represents.
One common mistake is trying to modify a const variable.
const int LIMIT = 50;
// LIMIT = 100; // Error
If a value needs to change during the program, it should not be declared as a const object.
Use constants for values that represent fixed rules or settings in your program.
const int MAX_STUDENTS = 50;
const int DAYS_IN_WEEK = 7;
const double GST_RATE = 0.18;
Meaningful names make these values easier to identify and maintain.
A constant can be declared inside a function and used in calculations.
#include <iostream>
void showPrice() {
const double PRICE = 500.0;
std::cout << PRICE;
}
int main() {
showPrice();
return 0;
}
#include <iostream>
int main() {
const double TAX_RATE = 0.18;
const double DISCOUNT = 100.0;
double price = 1000.0;
double tax = price * TAX_RATE;
double finalPrice = price + tax - DISCOUNT;
std::cout << "Price: " << price << std::endl;
std::cout << "Tax: " << tax << std::endl;
std::cout << "Final Price: " << finalPrice << std::endl;
return 0;
}
Here, TAX_RATE and DISCOUNT are fixed values, while price is a normal variable.
Constants are useful for values that should not be modified after initialization. In modern C++, const is commonly used to express this intent.
| Concept | Example |
|---|---|
| Integer constant | const int DAYS = 7; |
| Double constant | const double PI = 3.14159; |
| Character constant | const char GRADE = 'A'; |
| Boolean constant | const bool PASSED = true; |
| String constant | const std::string NAME = "Rahul"; |
Question: Which keyword is used to declare a constant object in C++?