const prevents modifying a value through that name. volatile tells the compiler the value may change unexpectedly—hardware registers, signal handlers, or shared memory.
const patterns
const int MAX = 100;
void print(const char *s); /* won't modify string via s */
const char *p — pointer to const char. char * const p — const pointer to mutable char.
volatile usage
volatile uint32_t *status_reg = (volatile uint32_t *)0x40001000;
Do not use volatile for thread synchronization—use atomics or mutexes instead.
Important interview questions and answers
- Q: const char* vs char const*?
A: Same—pointer to const char; the pointed-to chars cannot change through that pointer. - Q: volatile for multithreading?
A: Insufficient alone—prevents certain optimizations but does not provide atomicity or ordering guarantees.
Self-check
- Can you modify a const int local after initialization?
- When is volatile appropriate?
Tip: Read const int *p right-to-left: pointer to const int. int * const p is a const pointer to mutable int.
Interview prep
- volatile for threads?
Insufficient alone for synchronization—use atomics, mutexes, or platform thread APIs.
- const char* meaning?
Pointer to read-only char—cannot modify chars through that pointer.