Constants in C++
In C++, constants are fixed values that do not change during the execution of a program. They are also known as literals. Once a constant is defined, its value cannot be modified.
Constants are useful when you want to represent fixed values like mathematical
values
(pi = 3.14), maximum limits, or any value that should remain the same throughout the
program.
Syntax:
const data_type CONSTANT_NAME = value;
Why Use Constants?
- To make code readable and maintainable.
- To avoid accidental changes in values that should remain fixed.
- To represent fixed values like π = 3.14159, number of days in a week = 7, etc.
Types of Constants in C++
| Type | Description | Example |
|---|---|---|
| Integer Constants | Whole numbers without decimal point | 10, -25, 1000 |
| Floating Point Constants | Numbers with decimal point | 3.14, -0.75 |
| Character Constants | Single characters enclosed in single quotes | 'A', 'z', '9' |
| String Constants | Sequence of characters enclosed in double quotes | "Hello", "C++ Notes" |
| Boolean Constants | Represents truth values | true, false |
| Symbolic Constants | Constants defined using const keyword, #define
preprocessor, or enum |
const float PI = 3.14; #define MAX_LIMIT 100 |
Rules for Defining Constants
- Constants must be initialized at the time of declaration.
- Once assigned, their value cannot be changed.
- They can be defined using const keyword or #define directive.
- Constant names usually follow uppercase convention for better readability.
Example Program using Constants
// Demonstration of constants
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14159; // constant using const
#define MAX_LIMIT 100 // constant using #define
enum week { MON, TUE, WED, THU, FRI, SAT, SUN }; // Using enum
int radius = 5;
float area = PI * radius * radius;
cout << "Radius: " << radius << endl;
cout << "Area of Circle: " << area << endl;
cout << "Maximum Limit: " << MAX_LIMIT << endl;
cout << "Wednesday: " << WED ;
return 0;
}
Output:
Radius: 5
Area of Circle: 78.5398
Maximum Limit: 100
Wednesday: 2
Explanation:
PIis a constant declared using the const keyword.MAX_LIMITis a symbolic constant created using #define.weekis an enum which creates symbolic integer constantsMON=0, TUE=1, WED=2, ….- In the output,
WEDprints2because enum values start from 0 by default. - All these constants cannot be modified later in the program, improving readability and preventing accidental changes.
Real-Life Analogy:
Think of constants like your date of birth. Once it is set, it never changes throughout your life. Similarly, constants in C++ are fixed values that remain unchanged throughout program execution.
Common Mistakes with Constants
- Forgetting to initialize constants at declaration.
- Trying to modify a constant later in the program.
-
Confusing
'A'with"A"is a very common mistake:'A'→ This is a character constant. It represents a single character and is stored in a variable of typechar."A"→ This is a string constant. It represents a sequence of characters and is stored in astringorchar[].-
Rule: Single quotes (
' ') are used for single characters, whereas double quotes (" ") are used for strings. -
Example:
char ch = "A";(invalid, "A" is string)
char ch = 'A';(valid, 'A' is character)
- Using lowercase names for constants, which reduces readability.
Summary:
- Constants are fixed values that cannot be changed during program execution.
- Types include integer, float, char, string, boolean, and symbolic constants.
- Defined using
constor#define. - They improve program readability and prevent accidental modifications.