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

  1. Constants must be initialized at the time of declaration.
  2. Once assigned, their value cannot be changed.
  3. They can be defined using const keyword or #define directive.
  4. 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:

  • PI is a constant declared using the const keyword.
  • MAX_LIMIT is a symbolic constant created using #define.
  • week is an enum which creates symbolic integer constants MON=0, TUE=1, WED=2, ….
  • In the output, WED prints 2 because 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 type char.
    • "A" → This is a string constant. It represents a sequence of characters and is stored in a string or char[].
    • 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 const or #define.
  • They improve program readability and prevent accidental modifications.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar