volatile and const are two important keywords in C, but they solve very different problems. Beginners often confuse them because both are type qualifiers and both appear next to variable declarations. But one is about compiler behavior, and the other is about modification rules.
If you understand this difference properly, you can write safer code, avoid incorrect compiler assumptions, and handle embedded or low-level programs more correctly. In this article, we will understand volatile vs const in C, what each keyword means, how they differ, where they are used, and common mistakes.
What is const in C?
The const keyword in C means the value should not be modified through that variable or pointer after initialization.
consttells the programmer and the compiler that the object should be treated as read-only through that name.
Example:
const int x = 10;
/* x = 20; */Here x cannot be changed later in normal code. If you try to assign a new value, the compiler reports an error.
What is volatile in C?
The volatile keyword tells the compiler that the value of a variable may change unexpectedly, outside the normal visible flow of the program.
volatiletells the compiler not to assume the value stays unchanged between reads.
This matters because compilers optimize code aggressively. Without volatile, the compiler may store a value in a register, skip repeated reads, or reorder operations in ways that break hardware or signal-driven code.
Example:
volatile int status;This tells the compiler to read status from memory whenever needed instead of assuming the value is unchanged.
Volatile vs Const in C
The main difference is simple:
constis about preventing modification through that declarationvolatileis about preventing unsafe compiler assumptions about value stability
So these keywords do completely different jobs. One controls write intent. The other controls optimization assumptions.
Difference Between Volatile and Const in C Table
| Feature | const | volatile |
|---|---|---|
| Main purpose | Prevents modification through that name | Prevents the compiler from assuming the value stays unchanged |
| Focus | Programmer access rules | Compiler optimization behavior |
| Typical use | Read-only values, function parameters, constant tables | Hardware registers, flags changed by interrupts, shared state with external effects |
| Can value change in memory? | Maybe, through other means in some cases | Yes, that is the point |
| Stops writes? | Yes, through that declaration | No |
| Stops optimization-based caching? | No | Yes, for accesses to that object |
Example of const in C
#include <stdio.h>
int main(void) {
const int max_value = 100;
printf("%d\n", max_value);
return 0;
}This is useful when a value should remain unchanged in normal program logic.
Example of volatile in C
A common example is a flag updated by an interrupt or some external hardware event.
volatile int flag = 0;
while (flag == 0) {
/* wait */
}If flag is changed outside the visible code flow, the compiler must keep checking memory. Without volatile, it may optimize the loop incorrectly.
Why Volatile and Const Are Often Confused
- both are type qualifiers
- both appear in declarations near variable types
- both affect how the compiler treats the object
- both are common in embedded and systems programming
But the important point is that they affect completely different aspects of the program.
Can a Variable Be Both volatile and const in C?
Yes. A variable can be both volatile and const at the same time.
const volatile int sensor_reg;This means:
- the program should not modify the object through this declaration
- the value may still change unexpectedly because hardware or external logic may update it
This combination is common for memory-mapped hardware registers that software reads but should not write directly.
When to Use const in C
- for values that should not be modified
- for function parameters that should be read-only
- for lookup tables and fixed configuration data
- to make program intent clearer and safer
When to Use volatile in C
- for memory-mapped hardware registers
- for variables updated by interrupt service routines
- for values changed by signals or external agents
- for low-level synchronization points where repeated memory access matters
volatile is especially common in embedded programming, drivers, and hardware-facing code.
Common Mistakes in Volatile vs Const in C
- thinking
constmeans the value can never change anywhere - thinking
volatilemakes a variable read-only - using
volatileas a replacement for proper synchronization everywhere - forgetting
volatileon interrupt-updated flags - using neither keyword where hardware access rules clearly require one of them
These mistakes are common because the keywords sound simple, but their practical meaning depends on context.
Best Practices for Volatile vs Const in C
- use
constto protect read-only intent in normal program logic - use
volatileonly when external or non-obvious changes can happen - do not use
volatilejust to silence logic bugs - document hardware-related uses of
volatileclearly - use
const volatilewhen a value changes externally but should not be written through that declaration - review pointer declarations carefully because qualifier placement matters
FAQs
What is the main difference between volatile and const in C?
const prevents modification through that declaration, while volatile tells the compiler that the value may change unexpectedly and should not be assumed stable.
Can a variable be both const and volatile in C?
Yes. A variable can be both. This is useful when the program should only read the value, but the value may still change because of hardware or other external effects.
Does volatile make a variable constant in C?
No. volatile does not make a variable read-only. It only affects how the compiler handles accesses to that object.
Does const stop a value from changing in memory?
Not always in every low-level situation. It mainly stops modification through that declaration in normal C code. The object may still change through other mechanisms in some cases.