Constants in C++
In C++, a constant is a value that cannot be changed once it has been assigned. Unlike variables, whose values can be updated during program execution, constants always remain the same. They are useful when you have fixed values such as the value of pi, the number of days in a week, or the speed of light.
Using constants makes your code easier to understand and helps prevent accidental changes to important values. If a value should never be modified, it is a good practice to declare it as a constant.
Constant Variables in C++
The const keyword is used to create a constant variable. Once a value is assigned to a constant, it cannot be changed later in the program.
Syntax
const data_type variable_name = value;
Example
const float PI = 3.14159;
const int DAYS_IN_WEEK = 7;
In the above example, PI and DAYS_IN_WEEK are constant variables. Their values remain fixed throughout the program.
Modifying a Constant
Trying to change the value of a constant will result in a compilation error.
Invalid Example
const int age = 20;
age = 25;
The above code is invalid because age has been declared as a constant.
Types of Constants in C++
C++ provides different types of constants depending on the value they store.
Numeric Constants
These represent fixed integer or decimal values.
100
45.75
Character Constants
Character constants store a single character enclosed in single quotes.
'A'
'Z'
'5'
String Constants
String constants represent a sequence of characters enclosed in double quotes.
"Hello"
"CoderMantra"
Boolean Constants
Boolean constants have only two possible values.
true
false
C++ Constants with Examples
The following program demonstrates how constants are used in C++.
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14159;
float radius = 5;
float area = PI * radius * radius;
cout << "Area = " << area;
return 0;
}
Output
Area = 78.5397
In this example, the value of PI never changes, making it a perfect choice for a constant.
Why Use Constants?
Using constants offers several advantages:
- Prevents important values from being modified.
- Makes programs easier to read and maintain.
- Reduces the chances of programming errors.
- Improves code reliability by protecting fixed values.
Key Points
- Constants in C++ store values that cannot be changed after declaration.
- Use the
constkeyword to create a constant variable. - Constants can represent numeric, character, string, and boolean values.
- Modifying a constant is not allowed and causes a compilation error.
- Constants improve code readability and help write safer programs.
Frequently Asked Questions
Q1. What are constants in C++?
Constants in C++ are fixed values that do not change during program execution. They can represent numbers, characters, strings, or other predefined values.
Q2. What is the difference between a constant and a variable in C++?
A variable can store values that change during program execution, whereas a constant stores a fixed value that remains the same and cannot be modified after it is declared.
Q3. How do you declare a constant in C++?
You can declare a constant using the const keyword. For example, const int MAX = 100; creates a constant integer whose value cannot be modified.
Q4. What are the different types of constants in C++?
Common types of constants include integer constants, floating-point constants, character constants, string literals, and boolean constants.
Q5. Can the value of a constant be changed in C++?
No. Once a constant is declared and initialized, its value cannot be modified. Attempting to change it will result in a compilation error.
Q6. Why should I use constants in C++?
Using constants makes your code safer, easier to read, and easier to maintain by preventing accidental changes to values that should remain fixed.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
