Language[lang]
Const Variable Explained: What It Is and How to Use It
- Section
- Language [lang]
- Published
- Length
- 6 min read
In this answer4 clauses
- 1What a Const Variable Is
- 1.1Why a const value stays read-only
- 1.2Setting the value when you declare it
- 1.3How the compiler flags reassignment errors
- 1.4Short examples of const in action
- 2How to Write a Const Declaration
- 2.1Where the const keyword can go
- 3Why Using Const Pays Off
- 3.1Keeping code safer and easier to maintain
- 3.2Protecting function arguments and avoiding copies
- 3.3Where const usually shows up
- 4Combining Const with Pointers
- 4.1Pointing at a value you can’t change
- 4.2Locking the pointer but not its value
- 4.3Fixing both the pointer and its value
Abstract
A const variable is a value initialized once that C++ treats as read-only, so attempts to reassign it are rejected by the compiler. Use const for values that should stay fixed and const references for inputs a function only reads; with pointers, note whether const applies to the pointed-to value, the pointer, or both. In practice, const makes intent clearer and catches accidental changes before they become runtime bugs.

Think of a pantry label that says ‘do not refill.’ Everyone can read it, and nobody should swap out what’s inside. A const variable works the same way: once set, its value can’t change, and the compiler enforces that.
The keyword catches accidental writes before they become bugs.
We’ll cover what const means, how to write a declaration, and why it pays off in review. Then we’ll tackle pointer placement, const versus constexpr, linkage, and const_cast pitfalls.
Each answer names the standard it applies to, so you know which rules govern your code.
What a Const Variable Is
A const variable is a named value you set once and never change.
Why a const value stays read-only
The const keyword is a built-in type qualifier. Write const int limit = 10; and limit becomes read-only: a fixed value you can read, but assignments, increments, and += are all rejected.
That makes const less about where a value lives and more about whether anyone can change it. Consider a firmware developer who writes const int kBufferSize = 256;.
A teammate who later types kBufferSize = 512; during a late-night fix gets a build error in review, not a surprise on the device.
Setting the value when you declare it
A const variable gets its value at declaration, and that initializer is the only value it will ever hold. const int limit; with no initializer won’t compile.
Three keywords sound alike but answer different questions:
const: fixed after initialization, and the initializer can be computed at run time.constexpr: the initializer must be computable at compile time, and the variable is const.constinit: initialized at compile time for static or thread storage, but not const, so you can still modify it later.
How the compiler flags reassignment errors
If you try to change a const variable, the build fails. GCC (the GNU Compiler Collection) reports something like error: assignment of read-only variable 'limit'.
Because const is part of the language itself, the mistake is caught before the program runs. The compiler does the checking.
Save the example below as limit.cpp and run g++ -std=c++17 -c limit.cpp. The -c flag stops after compiling, so you see the error without linking.

Short examples of const in action
Here’s the illegal assignment:
int main() {
const int limit = 10;
limit = 20; // error: assignment of read-only variable 'limit'
}
— end example]
The PI constant is the classic case:
#include <iostream>
const double pi = 3.14159;
int main() {
std::cout << pi << '\n';
}
— end example]
Printed output:
3.14159
— end example]
Put pi in a header, and a game engine programmer who includes it in several files gets no multiple-definition error at link time. A namespace-scope const has internal linkage by default, so each .cpp file keeps its own private copy.
How to Write a Const Declaration
Both orders of the const keyword are legal, and you’ll meet each one in real code. Here’s what to write and how to read it.
Where the const keyword can go
You can put const before the data type, as in const int limit = 10;, or after it, as in int const limit = 10;. The compiler treats these identically: only the order changes, not the meaning.
The second placement is called east const, because the keyword sits to the right of the type it qualifies.
The preferred form in this guide is type const, where const follows the type: int const limit = 10;. The const-first form, const int limit = 10;, is the equivalent alternate syntax, and you’ll still see it in plenty of codebases, so you need to read both.
Read east const right to left, starting at the name: limit is a const int. Once that clicks, the order stops being a puzzle.
Picture a firmware developer reviewing a pull request where one file writes const int and another writes int const. She knows both mean the same thing, so she doesn’t stop to ask whether the change was intentional. Two spellings, one meaning.
Agree on a single style for the team, and reviews can focus on logic instead.

Why Using Const Pays Off
Adding const costs a few keystrokes but changes how code reads, how it’s reviewed, and what the compiler may assume.
Keeping code safer and easier to maintain
The gain is less about typing fewer characters and more about knowing what a value will do.
A const variable can’t change once it’s set, so a stray assignment in a long function becomes a compile error instead of a bug you chase later. Fewer bugs, more reliable code.
It also makes intent visible. A reviewer who sees const auto limit = config.maxRetries(); knows the value is fixed and can skip re-checking later uses. That’s easier maintenance and readability.
Protecting function arguments and avoiding copies
A const reference parameter lets a function read an argument without changing or copying it, so prefer it for any input you only read. Consider a game engine programmer whose countVisible helper takes a std::vector<Mesh> by value.
Each frame, the helper copies every mesh before its first line runs. Switching the parameter to const std:: leaves call sites unchanged, stops the copying, and gives the compiler more room to optimize.
Returning const is the one place it works against you. A move constructor expects a non-const rvalue, so a const return value blocks the move and falls back to a copy: you wanted a move and got a copy.
Skip const on return values unless you have a specific reason.

Where const usually shows up
You’ll find const on values that should never change while your code runs:
- Mathematical constants, like PI:
const double pi = 3.14159265358979; - Array sizes, so the size can’t drift from the array it describes
- Configuration values, such as a port number read at startup
Combining Const with Pointers
With pointers, the real question is which part is fixed. The answer depends on where const sits, and you can read it right off the declaration.
Pointing at a value you can’t change
Put const before the type, as in const int* p, and the value is read-only through p. This is low-level const. A game engine programmer might pass a rendering helper a const float* to a vertex buffer, so any write becomes a compile error.
With int n and int m in scope, *p = 3; fails, but p = &m; still compiles because the pointer itself isn’t const. Arrays work the same way: const int* first = arr; lets you walk a const int arr[3] without writing to it.
Const governs the view, not the object. If n isn’t const, you can still change it through n directly. The pointer is its own object with its own storage, so const on the pointee doesn’t change where p lives.
Locking the pointer but not its value
Move const after the star: int* const q = &n;. The pointer is now fixed, but *q = 4; still compiles. Repointing with q = &m; fails. This is top-level const, const on the pointer itself: the pointer is locked, and the value is not.
Fixing both the pointer and its value
const int* const r = &n; means neither the pointer nor the value can change: *r = 5; and r = &m; both fail. Read it right to left: r is a const pointer to a const int. The first const guards what the pointer reads, and the second guards where it points.
Casting away const with const_cast compiles, but if the object was defined const, writing through the cast pointer is undefined behavior. Strip const only from objects you know aren’t const.
Const is one of the cheapest ways to make code say what it means. It tells the compiler and your teammates which values shouldn’t change, and it turns many accidental writes into build errors.
Match each rule to the standard and compiler you actually build with. Next time you review a declaration, ask whether it should ever change. If it shouldn’t, say so with const.
Mark what should stay put, and let the compiler hold the line.
Questions
What is the difference between static and volatile variables in C?
In C, `static` affects a variable’s storage duration or visibility: a local static variable retains its value between function calls, while a file-scope static variable is limited to that source file. `volatile` tells the compiler that a value may change unexpectedly, such as through hardware or an interrupt, so it must not assume the value stays the same between accesses. They serve different purposes and can be used together.
What are the 7 types of operators in C?
Seven commonly taught categories are arithmetic, relational, logical, bitwise, assignment, conditional, and special operators. The exact grouping can vary by textbook; increment and decrement, for example, are often included under arithmetic operators.
How to make a const variable in Python?
Python does not have a built-in keyword that makes a variable constant. By convention, use an uppercase name such as `MAX_SIZE = 100`; tools such as type checkers can also use `Final` from `typing` to flag reassignment, but Python does not enforce immutability at runtime.
How do I declare a Boolean variable in C?
In C99 and later, include `<stdbool.h>` and declare a Boolean variable with `bool`, for example `bool is_ready = true;`. In C23, `bool`, `true`, and `false` are built into the language; earlier C versions can use `_Bool` directly.
How to write "I love you" in C language?
Use `printf` from `<stdio.h>` to print the phrase: `printf("I love you\n");`. Place it inside `main`, and include the header before using `printf`.
What is a static variable example?
A function-local static variable keeps its value between calls: `void count(void) { static int calls = 0; ++calls; }`. Each call to `count` increments the same `calls` variable rather than creating a fresh one.
What does const mean in coding?
In C and C++, `const` qualifies an object as read-only through that name or expression, so attempts to assign to it are rejected by the compiler. It does not always make the underlying object immutable through every possible access path, and `const` does not necessarily mean the value was determined at compile time.
When did C add bool?
C added Boolean support in the C99 standard, published in 1999. C99 introduced the `_Bool` type and the `bool`, `true`, and `false` names through the `<stdbool.h>` header.