Skip to content
Tutorials Center
My learning

Topics

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.

C
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

C const at different pointer levels

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.

C restrict and _Atomic

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 a const int.
  • int *const p: p is a const pointer to int.
  • const int *const p: a constant pointer to a constant int.
  • const int **pp: a pointer to a pointer to const int. Converting int ** to const int ** is not allowed implicitly because it would let you smuggle a const object's address into a non-const pointer.

Usage notes

  • const is not a compile-time constant in C. const int n = 5; int a[n]; makes a variable length array. Use an enum, a macro or C23 constexpr when you need a constant expression.
  • Modifying a const object (one defined const) through a cast is undefined behavior; it may live in read-only memory. Passing a pointer to a non-const object through a const * parameter is fine, and the function simply promises not to write.
  • volatile is not for threads. It does not make accesses atomic or provide ordering between threads. Use _Atomic or a mutex.
  • restrict is a promise to the compiler, not a checked rule. If dst and src overlap in the example above, the behavior is undefined. Library functions such as memcpy and strcpy use it; memmove does not because overlap is allowed there.
  • Qualifiers are dropped by casts and by value conversion: the value of a const int is just an int.
  • Qualifiers can combine: const volatile is legal and describes a read-only hardware register.
  • _Atomic can 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]) and void f(int a[const]) qualify the decayed pointer.