C++ Constants

A constant in C++ is a named value that cannot be changed after it is initialized. Constants are useful when a value should stay the same throughout a program.

You create most beginner constants with the const keyword. This tells the compiler to reject later attempts to assign a new value to that name.

Syntax

Write const, then the data type, the name, and the starting value:

const double taxRate = 0.08;

A constant must be initialized when it is declared. Unlike an ordinary variable, you cannot create it first and fill in the value later.

Example

#include <iostream>

int main(void) {
    const double price = 25.00;
    const double taxRate = 0.08;
    const int quantity = 3;

    double subtotal = price * quantity;
    double tax = subtotal * taxRate;
    double total = subtotal + tax;

    std::cout << "Subtotal: $" << subtotal << std::endl;
    std::cout << "Tax: $" << tax << std::endl;
    std::cout << "Total: $" << total << std::endl;

    return 0;
}

Output:

Subtotal: $75
Tax: $6
Total: $81

How It Works

const double price = 25.00; creates a constant named price. Its type is double, and its value is fixed at 25.00.

The program also creates taxRate and quantity as constants because those values are not meant to change while the calculation runs. The variables subtotal, tax, and total are ordinary variables because they store calculated results.

Using names such as taxRate is clearer than writing 0.08 directly in several places. If the rate needs to be different in a future version of the program, there is one obvious declaration to update.

Constants Cannot Be Reassigned

After a constant is initialized, assigning to it is an error. This program shows the idea in a comment:

#include <iostream>

int main(void) {
    const int maxScore = 100;

    std::cout << "Maximum score: " << maxScore << std::endl;

    // maxScore = 120; // Error: cannot assign to a const variable

    return 0;
}

Output:

Maximum score: 100

The commented line is not executed, so the example still compiles. If you remove the comment marks, the compiler reports an error because maxScore is constant.

When To Use Constants

Use const for values that have a clear meaning and should not accidentally change. Good examples include limits, fixed rates, conversion values, and settings used by a calculation.

  • Use const int maxAttempts = 3; for a fixed count.
  • Use const double pi = 3.14159; for a mathematical value.
  • Use const double centimetersPerInch = 2.54; for a conversion factor.

If a value is expected to change while the program runs, use an ordinary variable instead.

Naming Constants

C++ allows the same naming rules for constants and variables: letters, digits, and underscores, with no digit at the start. Many C++ programs use normal descriptive names such as maxScore or taxRate.

The most important rule is clarity. A name like daysInWeek explains the value better than a short name like d.

Common Mistakes

  • Forgetting to initialize a constant when declaring it.
  • Trying to assign a new value to a const variable later.
  • Using a constant for a value that really should change during the program.
  • Writing the same fixed number in many places instead of giving it a clear constant name.

Takeaway: use const when a named value should be initialized once and protected from accidental changes.