Learn C
#include "variablesdatatypes/const_volatile.h"

const and volatile in C

#concept

const is a compiler-enforced promise not to modify: const int LIMIT = 100; cannot be reassigned, and const char *p (pointer to const) forbids writes through p. It documents intent, enables optimizations, and turns accidental mutation into compile errors.

volatile is the opposite promise — about the compiler, not you: the value may change from outside the program's visible flow (hardware registers, interrupt flags, other threads), so every read must actually happen and no access may be optimized away. Embedded and systems code lives on volatile; application code rarely needs it.

#how it works
1

const locks the binding

const int LIMIT = 100; can be read but never assigned — violations fail at compile time, not in testing.

2

Read const-pointer declarations inside out

const char *p reads right-to-left: pointer to const char (can't modify through p); char * const p is a const pointer to mutable char.

3

volatile forbids optimization

volatile int flag; forces every read/write to really occur — the compiler may not cache it in a register or skip repeats.

4

Where each belongs

const everywhere a value is fixed (limits, table data, read-only parameters); volatile on hardware registers and cross-context flags.

#example
#include <stdio.h>
 
int main(void) {
const int limit = 10;
const char *msg = "hi";
printf("%d %s\n", limit, msg);
return 0;
}
Output
10 hi
1limit is fixed at 10 — any later assignment to it would fail compilation.
2msg points at read-only literal storage through a pointer-to-const; printing is allowed, writing through msg is not.
3Both print normally — const restricts writes, never reads.
#use cases
Locking configuration values and table data against accidental writes
Read-only string parameters as const char * throughout APIs
Memory-mapped hardware registers declared volatile in firmware
Reading const/volatile declarations right-to-left in reviews

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.