const Read-only type qualifier
const Read-only type qualifier
const bars lvalue modification; pointer-to-const differs from const pointer. It implies no read-only storage, and block const int cannot be a case label.
| Category | Type qualifiers |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
const int N = 100;
const int *p; /* Pointer to const */
int * const q; /* Const pointer */
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
10 20
20
🏷️ Related type qualifiers
📚 Related Tutorials
❓ FAQ
QWhat is the difference between const int *p and int * const p?
AWith const int *p, the pointed-to int cannot be modified through p, but p can point elsewhere. With int * const p, p itself cannot be redirected, though the non-const int it points to can still be modified. The forms can also be combined.
QCan an object be modified after casting away const?
AIf the original object was defined as const, modifying it is undefined behavior; a cast must not bypass that rule. If the original object is non-const and merely accessed through a pointer to const, another valid non-const lvalue may modify it.
QCan a local const int be used as a case label?
ANo. In C, the value of a const-qualified object is not an integer constant expression, even when its initializer is visible at compile time. Use an enumeration constant, a macro, or a direct constant expression instead.