C volatile Qualifier

Advanced
6 min read
Updated: Nov 2026
1 example

What Is volatile Qualifier?

volatile tells the compiler a value may change outside the normal flow of the program, so every access must really read or write memory. Typical uses are memory-mapped hardware registers and flags set by signal handlers (volatile sig_atomic_t). volatile does not provide thread safety or atomicity.

Remember
volatile int ready = 0;               /* re-read every time */
volatile sig_atomic_t got_signal = 0;     /* set by a signal handler */
while (!ready) { /* spin */ }

Cheat Sheet

Keep these facts handy while you work with volatile Qualifier in C.

TopicDetail
UseHardware registers, signal flags
EffectNo caching in registers
Not forThread synchronization
Handler typevolatile sig_atomic_t

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: restrict Qualifier.

Example 1 — A flag set by a signal handler

raise triggers the handler synchronously.

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

static volatile sig_atomic_t flag = 0;

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

int main(void) {
    signal(SIGINT, handler);
    raise(SIGINT);
    printf("handler ran: flag=%d\n", (int)flag);
    return 0;
}

How It Works

  1. signal installs handler for SIGINT.
  2. raise sends the signal, so the handler runs immediately.
  3. volatile ensures main re-reads the flag instead of assuming it is still 0.

Edge Cases & Pitfalls

Check these before relying on volatile Qualifier in real programs.

thread

Using volatile for threads

volatile does not make operations atomic or ordered. Use C11 _Atomic or a mutex.

handler

Doing real work in handlers

Signal handlers should only set a flag or call async-signal-safe functions.

Key Takeaways

  • Volatile: always re-read.
  • Signals and hardware.
  • Not synchronization.

One line: volatile tells the compiler a value may change outside the normal flow of the program, so every access must really read or write memory.

Frequently Asked Questions

It forces the compiler to perform every read and write of the object as written, because the value may change outside the program's control.
No. volatile only prevents certain compiler optimizations; it does not guarantee atomic or ordered access between threads.
Continue with "C restrict Qualifier" next. Promise the compiler that pointers do not overlap (C99). The C Advanced Concepts hub lists every topic in order.

Next: restrict Qualifier

Promise the compiler that pointers do not overlap (C99).

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful