0
0
Embedded Cprogramming~10 mins

Volatile keyword and why it matters in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Volatile keyword and why it matters
Declare volatile variable
Compiler reads variable
Variable value may change anytime
Compiler must NOT optimize reads/writes
Program uses latest variable value
Correct behavior with hardware/interrupts
The volatile keyword tells the compiler a variable can change anytime outside program control, so it must always read/write it fresh.
Execution Sample
Embedded C
volatile int flag = 0;

void interrupt_handler() {
  flag = 1;
}

int main() {
  while(flag == 0) {
    // wait
  }
  return 0;
}
This code waits until an interrupt sets 'flag' to 1. 'flag' is volatile to ensure the loop sees the change.
Execution Table
StepActionVariable 'flag' Value ReadCompiler Optimization Allowed?Effect on Program
1Initialize flag to 00No (volatile)flag is 0, loop starts
2Enter while loop, check flag0NoLoop continues waiting
3Interrupt occurs, sets flag to 11Noflag updated outside main flow
4Next loop check reads flag1NoLoop exits because flag == 1
5Program continues after loop1NoCorrect behavior, sees updated flag
💡 Loop exits when flag changes to 1, compiler reads fresh value each time due to volatile
Variable Tracker
VariableStartAfter Step 1After Step 3Final
flagundefined011
Key Moments - 2 Insights
Why can't the compiler optimize by caching 'flag' in a register inside the loop?
Because 'flag' is declared volatile, the compiler must read it from memory every time to catch changes made outside the program flow, like interrupts (see execution_table step 4).
What happens if 'flag' is NOT declared volatile?
The compiler might cache 'flag' in a register and never see the interrupt change, causing an infinite loop (compare execution_table step 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what value of 'flag' causes the loop to exit?
A-1
B0
C1
DUndefined
💡 Hint
Check step 4 and 5 where the loop condition changes and exits.
At which step does the interrupt change the value of 'flag'?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where 'flag' changes from 0 to 1 in the execution_table.
If 'flag' was not volatile, what might the compiler do during the loop?
ACache 'flag' in a register and never see changes
BSet 'flag' to 1 automatically
CAlways read 'flag' from memory
DIgnore the variable completely
💡 Hint
Refer to key_moments about compiler optimization and caching.
Concept Snapshot
volatile keyword in C:
- Declares variable can change unexpectedly
- Prevents compiler optimizations caching variable
- Ensures every read/write accesses actual memory
- Essential for hardware registers, interrupts
- Without volatile, program may behave incorrectly
Full Transcript
The volatile keyword tells the compiler that a variable's value can change at any time outside the normal program flow, such as by hardware or interrupts. This means the compiler must always read the variable from memory and not optimize by caching it in a register. In the example, a volatile flag variable is set to 0 initially. The program waits in a loop checking this flag. When an interrupt sets the flag to 1, the loop sees the change immediately and exits. Without volatile, the compiler might cache the flag value and never see the update, causing an infinite loop. This trace shows each step where the flag is read and updated, explaining why volatile matters for correct embedded programming.