Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

const-volatile

const and volatile

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches const and volatile: the syntax, patterns, and safety habits you need before advancing in C.

Teams still ship const and volatile in C codebases—skipping it leaves gaps in debugging and code reviews.

You will apply const and volatile in contexts like: Kernels, drivers, embedded devices, and performance libraries used by other languages.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

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

  1. Q: const char* vs char const*?
    A: Same—pointer to const char; the pointed-to chars cannot change through that pointer.
  2. Q: volatile for multithreading?
    A: Insufficient alone—prevents certain optimizations but does not provide atomicity or ordering guarantees.

Self-check

  1. Can you modify a const int local after initialization?
  2. 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.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • const pointer types?
  • volatile when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump