> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/02.-constant.md).

# 02. Constant

## 02. `const`

`const` makes a variable **read-only** after it's initialized. Once set, you can't change it — the compiler will throw an error if you try. Use it for values that should never change.

```cpp
#include <iostream>

int main() {
    const double PI = 3.14159;
    const int LIGHT_SPEED = 299;

    double radius = 42;
    double circumference = 2 * PI * radius;

    std::cout << circumference << " Cm" << std::endl;

    return 0;
}
```

***

### Why bother with `const`?

* **Prevents bugs** — you can't accidentally overwrite a value that should be fixed.
* **Documents intent** — anyone reading the code instantly knows "this never changes."
* **Compiler-enforced** — it's not just a convention, breaking it is a compile error, not a warning.

```cpp
const int MAX_USERS = 100;
MAX_USERS = 200; // ❌ compile error: assignment of read-only variable
```

***

### Naming convention

Constants are often written in `UPPER_SNAKE_CASE`. This is **not required** by the compiler — it's a human convention so constants stand out at a glance.

```cpp
const double GRAVITY = 9.81;   // clearly a constant
double gravity = 9.81;         // works too, but harder to spot
```

***

### More cool examples

#### 1. Circle calculations (area + circumference)

```cpp
#include <iostream>

int main() {
    const double PI = 3.14159;
    double radius = 7;

    double area = PI * radius * radius;
    double circumference = 2 * PI * radius;

    std::cout << "Area: " << area << '\n';
    std::cout << "Circumference: " << circumference << '\n';

    return 0;
}
```

#### 2. A simple discount calculator

```cpp
#include <iostream>

int main() {
    const double DISCOUNT_RATE = 0.20; // 20% off, never changes
    double price = 1500;

    double finalPrice = price - (price * DISCOUNT_RATE);

    std::cout << "Original price: " << price << '\n';
    std::cout << "You save: " << price * DISCOUNT_RATE << '\n';
    std::cout << "Final price: " << finalPrice << '\n';

    return 0;
}
```

#### 3. `const` with characters and strings

```cpp
#include <iostream>

int main() {
    const char GRADE_A = 'A';
    const std::string APP_NAME = "PixelForge";

    std::cout << APP_NAME << " gives you a grade: " << GRADE_A << '\n';

    return 0;
}
```

#### 4. Physics constants (multiple `const`s together)

```cpp
#include <iostream>

int main() {
    const double SPEED_OF_LIGHT = 299792458; // meters per second
    const double GRAVITY = 9.81;             // meters per second^2
    const int ABSOLUTE_ZERO_C = -273;        // Celsius

    std::cout << "Light speed: " << SPEED_OF_LIGHT << " m/s\n";
    std::cout << "Gravity: " << GRAVITY << " m/s^2\n";
    std::cout << "Absolute zero: " << ABSOLUTE_ZERO_C << " C\n";

    return 0;
}
```

#### 5. What happens if you try to break it (the error you'll see)

```cpp
#include <iostream>

int main() {
    const int LIVES = 3;
    LIVES = LIVES - 1; // ❌ error: assignment of read-only variable 'LIVES'

    return 0;
}
```

Compiling this gives something like:

```
error: assignment of read-only variable 'LIVES'
```

That error is `const` doing its job — protecting the value from accidental changes.

***

### Quick Recap

* `const TYPE name = value;` — locks the value at declaration.
* Trying to reassign it is a **compile-time error**, not a runtime bug — you catch it immediately.
* Convention: name constants in `UPPER_SNAKE_CASE`.
* Works with any type — `int`, `double`, `char`, `std::string`, etc.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/02.-constant.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
