const
const is a promise that you will not modify an object through this particular name or pointer. It constrains the access path, not necessarily the object, and in C it does not create a compile-time constant.
Detailed Explanation
const is best read as a promise, not as a property of memory. const int *p says: I will not write through p. It does not say the object cannot change. Someone else may hold a non-const pointer to the same object and modify it freely, and that is completely legal.
That distinction explains why the qualifier is useful on function parameters. When a function takes const char *text, the signature tells every caller that the string will be read and not rewritten. The compiler enforces it inside the function. That is documentation the compiler checks, which is the only kind that stays true.
The declaration forms trip people up because the meaning depends on which side of the * the keyword lands. Read declarations right to left from the name:
| Declaration | Read as | Can change the value? | Can repoint? |
|---|---|---|---|
const int *p | pointer to const int | no | yes |
int const *p | pointer to const int (identical) | no | yes |
int *const p | const pointer to int | yes | no |
const int *const p | const pointer to const int | no | no |
const int *p and int const *p mean exactly the same thing. Some style guides prefer int const * because right-to-left reading then works without an exception. Most existing C uses const int *. The rule that never changes: const to the right of the * qualifies the pointer.
C also allows const on the object itself: const int limit = 100;. Here the object really is const-qualified, and that is where the behavior differs sharply from a pointer promise. Modifying an object that was defined const is undefined behavior, even if you route around the type system with a cast. The compiler may have placed it in read-only memory, or folded its value into the code and never loaded it again.
Casting away const is therefore two different situations that look identical in source:
- The object is not const-qualified, and you only received a
constpointer to it. Casting awayconstand writing is defined. It breaks the contract you were handed, but the standard permits it. This is howstrchrcan return achar *into a string you passed asconst char *. - The object itself is const-qualified. Casting away
constand writing is undefined behavior, full stop.
Since the second case is invisible at the cast site, the practical rule is: do not cast away const. If you need write access, ask for a non-const pointer.
The last piece is the one that surprises anyone arriving from C++: const in C does not make a constant expression. const int SIZE = 8; is a read-only variable, not a compile-time constant. At file scope, int table[SIZE]; is an error, and so is a case SIZE: label or a bitfield width. C++ made const integers usable as constant expressions; C did not, and added constexpr only in C23. Inside a function, int table[SIZE]; does compile in C99 — but as a variable-length array, which is a different feature with different costs. For genuine compile-time constants use an enum constant or a #define.
The cost of const is close to zero. It occasionally helps the optimizer, mostly it helps readers and catches an accidental assignment at compile time, and it costs you some churn when you add it to an existing codebase because it propagates up through every caller.
Code Examples
#include <stdio.h>
/* const on a parameter is an enforced promise to the caller:
this function reads the buffer and never writes to it. */
static size_t count_char(const char *text, char target)
{
size_t found = 0;
for (const char *p = text; *p != '\0'; ++p) {
/* *p = 'x'; would be rejected here: p points to const char */
if (*p == target) {
found++;
}
}
return found;
}
int main(void)
{
char message[] = "mississippi"; /* not const: main may modify it */
printf("%zu\n", count_char(message, 's')); /* prints: 4 */
message[0] = 'M'; /* still legal: the object was never const */
printf("%s\n", message); /* prints: Mississippi */
return 0;
}#include <stdio.h>
/* enum constants and #define produce real compile-time constants.
A const int does not, which is the C/C++ difference that bites hardest. */
enum { CAPACITY = 4 };
static const int soft_limit = 4; /* a read-only variable, not a constant */
static int table[CAPACITY]; /* fine: CAPACITY is a constant expression */
/* static int broken[soft_limit]; rejected at file scope in every C standard */
int main(void)
{
int value = 10;
int other = 20;
const int *to_const = &value; /* cannot write through it, can repoint */
int *const const_ptr = &value; /* can write through it, cannot repoint */
/* *to_const = 11; rejected: pointer to const int */
to_const = &other; /* allowed */
*const_ptr = 11; /* allowed */
/* const_ptr = &other; rejected: the pointer itself is const */
printf("%d %d\n", value, *to_const); /* prints: 11 20 */
return 0;
}Common Gotchas
Modifying an object defined as `const` is undefined behavior
`const int limit = 5; *(int *)&limit = 6;` compiles and may appear to work, print `5` anyway, or fault if the object sits in read-only memory. The cast silences the compiler, not the standard.
`const` does not mean the value cannot change
it restricts one access path. Another pointer, another thread, or a hardware register may change the object underneath you. When the object really can change outside the program's control you want `volatile`, and the two combine as `const volatile`.
`const int n` is not a constant expression in C
it cannot size a file-scope array, appear in a `case` label, or set a bitfield width. Inside a function it appears to work only because C99 turns the declaration into a variable-length array. Use an `enum` constant or `#define`.
`const` on a by-value parameter affects nothing the caller can see
`void f(const int n)` promises only that `f` will not reassign its own copy. It is legal, occasionally useful as a local discipline, and never part of the interface.