Last time on Embed with Elliot we covered the static
keyword, which you can use while declaring a variable or function to increase the duration of the variable without enlarging the scope as you would with a global
variable. This piqued the curiosity of a couple of our readers, and we thought we’d run over another (sometimes misunderstood) variable declaration option, namely the volatile
keyword.
On its face, volatile
is very simple. You use it to tell the compiler that the declared variable can change without notice, and this changes the way that the compiler optimizes with respect to this variable. In big-computer programming, you almost never end up using volatile
in C. But in the embedded world, we end up using volatile
in one trivial and two very important circumstances, so it’s worth taking a look.