What volatile really does in C, and what it does not
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;
}
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
- No atomicity for read-modify-write.
flags |= 1uon a volatile variable is a read followed by a write. An interrupt between them can lose an update. Use a critical section, or hardware features such as bit-banding or set/clear registers. - No atomicity for wide types. A
volatile uint64_taccess on a 32-bit core may use multiple transfers, allowing an interrupt to observe a torn value. - No ordering against ordinary memory. The compiler may still move a non-volatile store across a volatile one. That is why the ring buffer uses acquire/release atomic indexes to order publication and reuse of buffer slots.
- No hardware barriers. On cores with write buffers or caches, you may also need
DSB/DMB, or an uncached memory region, for peripheral writes to take effect when you expect. - No thread safety on multi-core. For data shared across cores use C11 atomics or your RTOS primitives.
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
- Memory-mapped registers: always
volatile. - 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.
- Anything more complex than a single flag or index: use a critical section or an atomic type, not just
volatile. - Never use
volatileas 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.
| Situation | Suitable starting point |
|---|---|
| Read a hardware register | Volatile-qualified access using the vendor's documented width |
| Publish a flag between C threads | Atomic flag with suitable ordering |
| Increment a counter from two tasks | Atomic read-modify-write or a lock |
| Share a buffer with DMA | Target-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.
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.
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.