Type qualifiers
suggest changeC provides four standard type qualifiers:
| Qualifier | Purpose |
|---|---|
const |
prevents modification through an lvalue with that qualified type |
volatile |
makes accesses observable for objects that can change outside ordinary execution |
restrict |
expresses an aliasing contract for certain pointer-based accesses |
_Atomic |
gives an object atomic access semantics |
const does not necessarily mean that the underlying object can never change. It means that a particular lvalue cannot be used to modify it:
int value = 1;
const int *view = &value;
value = 2; /* Valid. */
/* *view = 3; */ /* Constraint violation. */
Qualification can apply to the pointer, the pointed-to type, or both:
const char *first; /* Pointer to const char. */
char *const second = 0; /* Const pointer to char. */
const char *const third = 0;
volatile does not make an operation atomic and does not synchronize threads. Its primary uses include implementation-defined memory-mapped I/O and the limited communication permitted with signal handlers.
restrict is a promise about how an object is accessed during an execution of a block. Violating that promise causes undefined behavior. It is more than an optimization hint, and its rules are subtler than “there can be no other pointer.”
_Atomic was introduced in C11. The typedefs and operations in <stdatomic.h> are usually clearer than spelling complex atomic-qualified types directly.