Lesson 10 of 60 – Constants in C++
17%

Constants in C++

A constant is a value that is not intended to change during the execution of a program. In C++, the const keyword is commonly used to create objects whose values cannot be modified after initialization.

Note: A const variable must be initialized when it is declared because its value cannot be assigned later.

1. What is a Constant?

A constant is a value that should remain unchanged after it has been initialized.

const int age = 20;

Here, age is a constant integer variable. Its value cannot be changed after initialization.

2. Why Use Constants?

Constants are useful when a value should remain fixed throughout a program or a particular scope.

  • Prevent accidental changes
  • Make code easier to understand
  • Represent fixed values
  • Improve code safety
  • Make programs easier to maintain

3. The const Keyword

The const keyword tells the compiler that an object cannot be modified through that object after initialization.

const int DAYS = 7;

The value of DAYS cannot be changed later.

4. Declaring a Constant

The general syntax for declaring a const variable is:

const data_type variable_name = value;

Example:

const int MAX = 100;

5. Constant Must Be Initialized

A const object normally needs an initializer when it is declared.

const int number = 10;

Here, the value 10 is assigned at initialization.

6. Cannot Change a const Variable

After initialization, a const variable cannot be assigned a new value.

const int age = 20;

// age = 25;   // Error

The assignment is not allowed because age is const.

7. Constant Integer

An integer can be declared as a constant using const.

const int DAYS_IN_WEEK = 7;

The value remains 7.

8. Constant Double

Floating-point values can also be declared as constants.

const double PI = 3.14159;

This is useful for mathematical values that should remain fixed.

9. Constant Character

A character can also be declared as a constant.

const char GRADE = 'A';

The character stored in GRADE cannot be changed.

10. Constant Boolean

A Boolean value can also be made constant.

const bool PASSED = true;

The value remains true.

11. Constant String

A std::string object can also be declared as const.

#include <string>

const std::string COLLEGE = "Soopro Pathshala";

The string object cannot be assigned a different string after initialization.

12. Printing a Constant

A constant can be displayed using std::cout, just like an ordinary variable.

#include <iostream>

int main() {

    const int DAYS = 7;

    std::cout << DAYS;

    return 0;
}

13. Constants in Calculations

Constants can be used in mathematical calculations.

const int PRICE = 100;
int quantity = 5;

int total = PRICE * quantity;

The constant PRICE is used to calculate the total amount.

14. const with Arithmetic Expressions

Constants can be used inside expressions with other variables.

const double TAX = 0.18;
double price = 1000;

double taxAmount = price * TAX;

15. Multiple Constants

A program can contain many constants.

const int DAYS = 7;
const int MONTHS = 12;
const double PI = 3.14159;

Each constant can represent a different fixed value.

16. const Variables and User Input

A const variable cannot be used for ordinary user input when its value needs to be changed after initialization.

const int age = 20;

// std::cin >> age;   // Error

User input normally requires a non-const variable.

17. const and Assignment

A normal variable can be assigned a new value. A const variable cannot.

int age = 20;
age = 25;             // Allowed

const int year = 2026;
// year = 2027;       // Error

18. Naming Constants

Constant names can follow the same identifier rules as other C++ variables.

A common convention is to use uppercase letters with underscores:

const int MAX_SIZE = 100;
const double TAX_RATE = 0.18;

Uppercase naming is a convention, not a requirement of the language.

19. Constant Names are Case-Sensitive

C++ is case-sensitive, so these are different identifiers:

const int MAX = 100;
const int Max = 200;
const int max = 300;

It is better to use a consistent naming convention.

20. Local Constants

A constant declared inside a function has local scope.

#include <iostream>

int main() {

    const int DAYS = 7;

    std::cout << DAYS;

    return 0;
}

The constant DAYS is available within its scope.

21. Global Constants

A const variable can also be declared at namespace scope.

#include <iostream>

const int MAX_SCORE = 100;

int main() {

    std::cout << MAX_SCORE;

    return 0;
}

Such a constant can be used where its scope makes it visible.

22. const with auto

The const keyword can be combined with auto. The compiler deduces the type from the initializer.

const auto age = 20;
const auto price = 99.50;

The resulting objects are const and cannot be modified.

23. const in a Simple Program

#include <iostream>

int main() {

    const double PI = 3.14159;
    double radius = 5;

    double area = PI * radius * radius;

    std::cout << "Area = " << area;

    return 0;
}

The value of PI remains fixed while the radius can be changed.

24. Constant vs Normal Variable

Normal Variable Constant
Can normally be changed Cannot normally be changed after initialization
Example: int age = 20; Example: const int AGE = 20;
Can receive later assignments Cannot receive a new value after initialization

25. Constants Improve Readability

Using a meaningful constant name can make code easier to understand.

Instead of:

double total = price * 0.18;

You can write:

const double TAX_RATE = 0.18;

double total = price * TAX_RATE;

The name TAX_RATE clearly explains what the value represents.

26. Common Mistake with Constants

One common mistake is trying to modify a const variable.

const int LIMIT = 50;

// LIMIT = 100;   // Error

If a value needs to change during the program, it should not be declared as a const object.

27. Choosing Constant Values

Use constants for values that represent fixed rules or settings in your program.

const int MAX_STUDENTS = 50;
const int DAYS_IN_WEEK = 7;
const double GST_RATE = 0.18;

Meaningful names make these values easier to identify and maintain.

28. Constants in Functions

A constant can be declared inside a function and used in calculations.

#include <iostream>

void showPrice() {

    const double PRICE = 500.0;

    std::cout << PRICE;
}

int main() {

    showPrice();

    return 0;
}

29. Complete Constants Example

#include <iostream>

int main() {

    const double TAX_RATE = 0.18;
    const double DISCOUNT = 100.0;

    double price = 1000.0;

    double tax = price * TAX_RATE;
    double finalPrice = price + tax - DISCOUNT;

    std::cout << "Price: " << price << std::endl;
    std::cout << "Tax: " << tax << std::endl;
    std::cout << "Final Price: " << finalPrice << std::endl;

    return 0;
}

Here, TAX_RATE and DISCOUNT are fixed values, while price is a normal variable.

30. Constants – Final Summary

Constants are useful for values that should not be modified after initialization. In modern C++, const is commonly used to express this intent.

Concept Example
Integer constant const int DAYS = 7;
Double constant const double PI = 3.14159;
Character constant const char GRADE = 'A';
Boolean constant const bool PASSED = true;
String constant const std::string NAME = "Rahul";

📌 Key Points

  • A constant represents a value that should not be modified after initialization.
  • The const keyword is commonly used to create const objects.
  • A const variable normally needs to be initialized when declared.
  • A const variable cannot be assigned a new value after initialization.
  • Constants can be integers, floating-point values, characters, Boolean values, strings, and other types.
  • Uppercase names such as MAX_SIZE are a common naming convention for constants.
  • C++ identifiers are case-sensitive.
  • Meaningful constant names improve readability.
  • Constants are useful for fixed values such as limits, rates, and mathematical values.
  • The const qualifier expresses that an object should not be modified through that name.

🧠 Quick Quiz

Question: Which keyword is used to declare a constant object in C++?