> For the complete documentation index, see [llms.txt](https://pllug-community.gitbook.io/pllug-c-qt-roadmap-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pllug-community.gitbook.io/pllug-c-qt-roadmap-book/mova-s++-shvidkii-vstup-ta-obrani-temi/klyuchove-slovo-const.md).

# Ключове слово const

Ключове слово `const` застосовують для оголошення змінних, які не можуть бути змінені. Його пишуть перед типом змінної під час оголошення. Наприклад, у одному з попередніх прикладів, ми оголошували змінну, яка містить пароль, який програма вимагатиме від користувача.

```cpp
const std::string cMySecretPassword{"qwerty"};
```

У цьому прикладі ми не хочемо, щоб значення `cMySecretPassword` змінювалося. Тому застосували ключове слово `const`. Тепер якщо, у ми програмі присвоємо значення змінній `cMySecterPassword`(наприклад, випадково, помилившись у написанні оператора порівняння `==`), то отримаємо помилку компілятора.

Також вартує використовувати константи кожного разу, коли ви задаєте чи порівнюєте значення, які часто зустрічаються у вашому коді чи потенційно можуть змінитися. Наприклад, якщо ми пишемо програму, для роботи з математичними матрицями, то можемо оголосити окремі константи для визначення розмірності. І вже даолі у програмі оголосити матрицю заданих розмірів:

```cpp
const int cRowCount{3};
const int cColumnCount{3};
...
std::array<std::array<double, cColumtCount>, cRowCount> matrix;
```

{% hint style="info" %}
[Уникайте "магічних" чисел, текстових рядків та інших "магічних" значень. Давайте значенням імена, використовуючи константи.](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es45-avoid-magic-constants-use-symbolic-constants)
{% endhint %}


---

# 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:

```
GET https://pllug-community.gitbook.io/pllug-c-qt-roadmap-book/mova-s++-shvidkii-vstup-ta-obrani-temi/klyuchove-slovo-const.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
