C Reference ยท Types and Literals
Type qualifiers in C
Reference for the four C type qualifiers const, volatile, restrict and _Atomic: what each promises, where it may appear, and how qualifiers combine with pointers.
A type qualifier changes how an object may be accessed without changing what kind of value it holds. C has four: const, volatile, restrict and _Atomic. They can be combined, and they can appear at several levels of a pointer type, which is where most confusion comes from.
Qualifiers are promises. const promises you will not write through this name, restrict promises that no other pointer reaches the same object, volatile tells the compiler that accesses have effects it cannot see, and _Atomic makes accesses indivisible. Breaking a promise is often undefined behavior.
const int limit = 10; /* cannot be assigned to */ volatile int ready; /* every access really happens */ void copy(int *restrict dst, const int *restrict src, int n); _Atomic int counter; /* atomic reads and writes */
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.
The four qualifiers
| Qualifier | Meaning | Typical use |
|---|---|---|
const |
The object may not be modified through this lvalue. | Read-only parameters, tables, constants. |
volatile |
Each access is observable behavior; the compiler may not remove, merge or reorder it relative to other volatile accesses. | Memory-mapped hardware registers, variables changed by a signal handler (volatile sig_atomic_t). |
restrict |
Only valid on pointers. During the pointer's scope, the object it points to is accessed only through that pointer (or pointers derived from it). | Allowing vectorization and load/store reordering. |
_Atomic |
Accesses are indivisible and take part in the memory model. | Counters and flags shared between threads. |
Reading pointer declarations
Qualifiers apply to the thing on their left (or, at the start, to the type on their right). Read right to left:
const int *p: p is a pointer to aconst int.int *const p: p is aconstpointer toint.const int *const p: a constant pointer to a constant int.const int **pp: a pointer to a pointer toconst int. Convertingint **toconst int **is not allowed implicitly because it would let you smuggle aconstobject's address into a non-const pointer.
Usage notes
constis not a compile-time constant in C.const int n = 5; int a[n];makes a variable length array. Use anenum, a macro or C23constexprwhen you need a constant expression.- Modifying a
constobject (one definedconst) through a cast is undefined behavior; it may live in read-only memory. Passing a pointer to a non-const object through aconst *parameter is fine, and the function simply promises not to write. volatileis not for threads. It does not make accesses atomic or provide ordering between threads. Use_Atomicor a mutex.restrictis a promise to the compiler, not a checked rule. Ifdstandsrcoverlap in the example above, the behavior is undefined. Library functions such asmemcpyandstrcpyuse it;memmovedoes not because overlap is allowed there.- Qualifiers are dropped by casts and by value conversion: the value of a
const intis just anint. - Qualifiers can combine:
const volatileis legal and describes a read-only hardware register. _Atomiccan be a qualifier or a specifier:_Atomic int x;and_Atomic(int) x;are the same. Include<stdatomic.h>for the operations. Atomic types may be unavailable on freestanding targets.- Array parameters:
void f(int a[restrict])andvoid f(int a[const])qualify the decayed pointer.
Related
- const, volatile, restrict, stdatomic.h, const correctness
- Lessons: const and pointers, Constants