C Programming · Interview question

Is volatile const a contradiction?

A strong answer

No, they constrain different parties. volatile says "this value can change outside the program's control, so re-read it every access." const says "this code may not write it." A read-only hardware register, one the hardware updates but firmware must only read, like a data-input or status register, is exactly volatile const: the hardware (outside the language's view) writes it, so you need volatile to see fresh values, and your code must not write it, so const catches an accidental store. There's no conflict because const restricts the program, while volatile describes the world outside the program. You'll see this combination throughout vendor register headers for input-only registers.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

const, volatile & static

Three small keywords with outsized consequences: const for intent, volatile for hardware and ISR-shared data, static for lifetime and linkage, and why volatile is not atomic.

More const, volatile & static questions

Browse all 472 interview questions