Brendan Ang

Search

Search IconIcon to open search

C

Last updated Nov 8, 2022 Edit Source

# C Programming

# Special keywords

# Volatile

1
int volatile foo;

Volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time-without any action being taken by the code the compiler finds nearby.

# Use in peripheral registers

These registers may have their values changed asynchronously during program flow. Code without this keyword can be optimised by the compiler into an infinite loop.

1
2
3
4
5
UINT1 * ptr = (UINT1 *) 0x1234;

// Wait for register to become non-zero.  
while (*ptr == 0);  
// Do something else.

Compiler interprets the ptr value is being always 0, as it has already loaded the value in the second line, resulting in an infinite loop:

1
2
3
mov ptr, #0x1234
mov a, @ptr
loop bz loop

Same situations can occur for variables that may be modified in ISRs or by multi-threaded applications.