C Reference ยท Keywords and Statements
The const qualifier
Reference for the C const qualifier: read-only variables, pointer to const versus const pointer, const parameters, and why modifying a const object is undefined.
const is a type qualifier that says an object must not be modified through that name. The compiler rejects assignments to it, and it may place the object in read-only memory. Unlike a #define constant, a const object has a type, an address and obeys scope rules.
Where const appears in a pointer declaration changes the meaning. Read the declaration from the name outward: const int *p is "pointer to const int" (the int is read-only), while int *const p is "const pointer to int" (the pointer is fixed).
const int max = 100; const char *name; /* data read-only, pointer changeable */ char *const buf; /* pointer fixed, data changeable */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
const type name = initializer; const type *name; /* pointer to const type */ type *const name; /* const pointer to type */
| Declaration | Can change the pointer? | Can change the pointed-to data? |
|---|---|---|
int *p |
yes | yes |
const int *p (or int const *p) |
yes | no |
int *const p |
no | yes |
const int *const p |
no | no |
Errors / undefined behavior: assigning to a const object through its own name is a compile error. Casting const away and then modifying an object that was defined const is undefined behavior (typically a crash for objects in read-only memory). The following read-only snippet shows it:
const int x = 5; int *p = (int *)&x; *p = 6; /* undefined behavior */
Usage notes
- A
constobject is not a constant expression in C.const int n = 4; int a[n];makes a variable length array (see VLAs); use anenum,#defineor C23constexprwhen a true constant is required. - Must be initialized at definition; at block scope without an initializer its value is indeterminate and cannot be set later.
- Adding
constis implicit (int *converts toconst int *); removing it needs a cast and is almost always a bug. Note thatchar **does not convert toconst char **. - String literals have type
char[]in C, notconst char[], but modifying them is undefined behavior. Declare pointers to themconst char *. constparameters document intent and allow the caller to pass read-only data.conston a by-value parameter itself (int f(const int x)) only restricts the function body.- Struct members can be
const, which makes assigning the whole struct invalid. - Optimization: the compiler may assume a
constobject never changes. Usevolatileas well for hardware registers that change on their own.