Introduction to Constants and Literals
In programming, not all values are meant to change during the execution of a program. Some values remain the same from the beginning to the end. For example, the value of π (pi), the number of days in a week, or a fixed tax rate. To handle such values safely and clearly, C++ provides the concepts of constants and literals.
Constants and literals play a very important role in writing reliable, readable, and error-free programs. Beginners often ignore these concepts, but professional programmers use them extensively to avoid mistakes and improve code quality.
This lesson explains constants and literals in very simple English, with deep explanation suitable for Class 5 to BS level students.
What Are Constants
A constant is a variable whose value cannot be changed after it is assigned.
In simple words:
A constant stores a value
That value remains fixed
The program is not allowed to modify it
Once a value is stored in a constant, it stays the same throughout the program.
Why Constants Are Needed
Constants are needed for many important reasons:
First, constants protect important values from being changed accidentally.
Second, they make programs easier to understand because fixed values are clearly marked.
Third, constants improve program safety and reduce logical errors.
Fourth, they help maintain consistency when the same value is used many times.
For example, if a program uses the value of pi in many calculations, changing it accidentally would produce incorrect results. Using a constant prevents this problem.
The const Keyword in C++
What Is the const Keyword
In C++, the const keyword is used to declare constants.
It tells the compiler:
This value is fixed
Do not allow any changes to it
Syntax:
Example of const
Here:
daysInWeekis a constantIts value is
7This value cannot be changed later
If you try:
The compiler will give an error.
Characteristics of Constants
Constants have the following properties:
Must be initialized at declaration
Cannot be modified later
Improve code readability
Prevent accidental changes
Importance of Initializing Constants
Unlike normal variables, constants must be initialized at the time of declaration.
Incorrect:
Correct:
This rule ensures that constants always have a valid value.
When and Why to Use Constants
When to Use Constants
You should use constants when:
A value should never change
The same value is used multiple times
A value represents a fixed rule or limit
You want to avoid hard-coded values
Examples:
Maximum marks
Speed of light
Fixed conversion rates
Mathematical constants
Why Constants Are Better Than Variables
Using constants instead of variables provides many benefits:
Prevents accidental modification
Makes code self-explanatory
Improves program reliability
Easier maintenance
Bad practice:
Good practice:
What Are Literals
A literal is a fixed value that is directly written in the program.
In simple words:
Literals are the actual values used in code.
Examples:
103.14'A'true"Hello"
These values are not stored in variables; they appear directly in the code.
Types of Literal Values in C++
Integer Literals
Integer literals are whole numbers without decimal points.
Examples:
They are used when exact whole numbers are needed.
Floating-Point Literals
Floating-point literals contain decimal points.
Examples:
They are commonly used in calculations involving fractions.
Character Literals
Character literals represent a single character enclosed in single quotes.
Examples:
They store characters using ASCII values internally.
String Literals
String literals are a sequence of characters enclosed in double quotes.
Examples:
Strings are used to display messages or store text.
Boolean Literals
Boolean literals represent logical values.
Examples:
They are used in conditions and decision-making.
Difference Between Constants and Literals
Although constants and literals may look similar, they are not the same.
Literals:
Are fixed values written directly
Do not have names
Cannot be reused easily
Constants:
Have names
Store fixed values
Can be reused many times
Example:
Here:
100is a literalmaxMarksis a constant
Common Mistakes with Constants and Literals
Using Variables Instead of Constants
Many beginners use variables for fixed values, which is unsafe.
Forgetting to Initialize Constants
Constants must always be initialized.
Confusing Literals with Variables
Writing:
is incorrect because literals cannot store values.