EmbedwrightArticlesLesson guideFree lessonsThe course · $79

What volatile really does in C, and what it does not

By Chinmay Gupta · Updated 2026-12-03
Short answer. volatile makes accesses to the qualified object observable under the compiler's documented rules. It does not make operations atomic, establish inter-thread ordering or insert hardware barriers.

Every embedded C engineer learns "use volatile for registers and for variables shared with interrupts". That is correct, and it is only half the story. The other half is what volatile does not promise, which is where real bugs live.

What the C standard says

Accessing a volatile-qualified object is part of the program's observable behavior. The compiler must perform each access written in the source, in the order required by C sequencing rules relative to other volatile accesses, and may not assume the value stays the same between two reads. What counts as an "access" is partly implementation-defined, so check your compiler's documentation for exotic cases such as accesses through a struct member.

That is why this loop is a bug without volatile:

#include <stdint.h>

#define UART_STATUS (*(volatile uint32_t *)0x40011000u)
#define RX_READY    (1u << 5)

void wait_for_byte(void)
{
    while ((UART_STATUS & RX_READY) == 0u) {
        /* each pass must really read the register */
    }
}

Compiles cleanly with -Wall -Wextra -Werror (not run on a host).

Without volatile the compiler may read the register once, see the bit clear, and turn the loop into an infinite one, because as far as the C abstract machine knows nothing in the loop changes it.

A flag set from an interrupt (or signal) handler

A standard C signal-handler flag can use volatile sig_atomic_t. This host example uses the POSIX signal SIGUSR1; MCU interrupt behavior is defined by the target, not ISO C:

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

static volatile sig_atomic_t got_event;

static void handler(int sig)
{
    (void)sig;
    got_event = 1;
}

int main(void)
{
    signal(SIGUSR1, handler);
    raise(SIGUSR1);                 /* handler runs before raise returns */
    printf("flag seen by main loop: %d\n", (int)got_event);
    return got_event == 1 ? 0 : 1;
}
Real output
flag seen by main loop: 1

Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.

sig_atomic_t is the one integer type the standard guarantees can be read and written atomically with respect to a signal handler. On many 32-bit MCUs, a suitably aligned volatile uint32_t load or store is a single instruction, but atomicity depends on the target, memory type and toolchain; it is not an ISO C guarantee.

What volatile does not do

Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1

A short checklist

  1. Memory-mapped registers: always volatile.
  2. Variable shared between main code and an ISR: use documented ISR-safe atomic operations or a target-specific critical section. A volatile flag alone is suitable only when the target and access pattern guarantee atomicity and the required ordering.
  3. Anything more complex than a single flag or index: use a critical section or an atomic type, not just volatile.
  4. Never use volatile as a substitute for a lock.

This is one of the questions interviewers ask most often. See firmware interview questions with answers for how to phrase it.

Volatile vs atomic: choose the right tool

Think of a memory-mapped status register as a notice board that hardware can update. Each read must observe the current register rather than reuse an earlier compiler-cached value. A shared counter is different: two contexts can both read the same value before either writes, losing an increment. Neither the volatile qualifier nor a naturally aligned load makes the whole increment indivisible.

SituationSuitable starting point
Read a hardware registerVolatile-qualified access using the vendor's documented width
Publish a flag between C threadsAtomic flag with suitable ordering
Increment a counter from two tasksAtomic read-modify-write or a lock
Share a buffer with DMATarget-specific ownership and cache maintenance

Try the shared-counter experiment in the free Firmware Visual Lab, then compare volatile and atomic in C. A register may also have read-to-clear behavior, so consult its documentation rather than polling blindly.

Interview check

Does volatile int count make count++ thread-safe? No. The expression reads, computes and writes; an atomic fetch-add or appropriate locking is needed for a shared counter. Even an atomic counter does not automatically protect unrelated state.

The course goes further on this topic. Lesson 6 of DSA for Firmware, “The flash that filled up”, adds interactive labs and C exercises with tests that you run on your own machine. Read the lesson guide · See the course
Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
DSA for Firmware teaches it one real bug at a time.
  • 28 lessons, each starting from a real firmware bug.
  • 110 C exercises with tests, and a reference solution for every one.
  • Lessons 1 and 2 are free. No sign-up.
Start Lesson 1 freeSee the full course · $79

Get the next article by email

One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.