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:

 
const dataType constantName = value;

Example of const

 
const int daysInWeek = 7;

Here:

  • daysInWeek is a constant

  • Its value is 7

  • This value cannot be changed later

If you try:

 
daysInWeek = 8;

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:

 
const int x;

Correct:

 
const int x = 10;

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:

 
int pi = 3.14;

Good practice:

 
const double PI = 3.14;

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:

  • 10

  • 3.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:

 
10
-25
0

They are used when exact whole numbers are needed.

Floating-Point Literals

Floating-point literals contain decimal points.

Examples:

 
3.14
99.99
-0.5

They are commonly used in calculations involving fractions.

Character Literals

Character literals represent a single character enclosed in single quotes.

Examples:

 
'A'
'9'
'#'

They store characters using ASCII values internally.

String Literals

String literals are a sequence of characters enclosed in double quotes.

Examples:

 
"Hello"
"C++ Programming"

Strings are used to display messages or store text.

Boolean Literals

Boolean literals represent logical values.

Examples:

 
true
false

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:

 
const int maxMarks = 100;

Here:

  • 100 is a literal

  • maxMarks is 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:

 
10 = x;

is incorrect because literals cannot store values.