A program waits with while(!flag); and an interrupt routine sets flag. The flag is declared without volatile and the loop never ends, although the routine ran. What is the most likely cause?
- (A)
The compiler read the flag once into a register and never read memory again
- (B)
A routine cannot write to a global variable
- (C)
A flag must be stored in program memory
- (D)
Interrupts are turned off while a loop runs
Answer and solution
Answer: (A)
Nothing in the loop changes flag, so the compiler may keep it in a register.
The routine writes memory, but the loop never reads memory again; volatile would force a fresh read.
B, C and D are false statements.