Skip to content
  • Type Qualifiers: const, volatile, restrict · C

Type Qualifiers: const, volatile, restrict

LearnViaNotes @learnvianotes· 2 min read

Qualifiers add promises about an object: const — I won’t write it; volatile — it may change behind the compiler’s back; restrict — this pointer is the only way to reach that memory.

QualifierPromise / meaningTypical use
constNot modified through this nameRead-only parameters, lookup tables
volatileEvery read and write must really happen, in order — no caching in registersMemory-mapped hardware registers, variables changed by signal handlers
restrict (C99)For the pointer’s lifetime, the object is accessed only through itmemcpy, numeric kernels — enables vectorisation
_Atomic (C11)Accesses are atomicLock-free counters shared between threads

volatile

C
#include <stdio.h>
#include <signal.h>

static volatile sig_atomic_t stop = 0;    /* changed by a signal handler */

void on_signal(int sig) { (void)sig; stop = 1; }

int main(void) {
    signal(SIGINT, on_signal);
    long spins = 0;
    while (!stop) {                        /* volatile: re-read every iteration */
        if (++spins == 1000000) raise(SIGINT);
    }
    printf("stopped after %ld spins\n", spins);
    return 0;
}
Output
stopped after 1000000 spins

restrict

C
#include <stdio.h>

/* the caller promises dst and src do not overlap, so the compiler may vectorise */
void add_arrays(size_t n, float *restrict dst, const float *restrict src) {
    for (size_t i = 0; i < n; i++) dst[i] += src[i];
}

int main(void) {
    float a[4] = {1, 2, 3, 4}, b[4] = {10, 20, 30, 40};
    add_arrays(4, a, b);
    printf("%.0f %.0f %.0f %.0f\n", a[0], a[1], a[2], a[3]);
    return 0;
}
Output
11 22 33 44

The standard library uses it: void *memcpy(void *restrict dst, const void *restrict src, size_t n); — which is precisely why overlapping memcpy is undefined and memmove (no restrict) exists.

Can a variable be both const and volatile?

What happens if you break a restrict promise?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report