Are persistent variables not widely used? I couldn’t find much info about them online or in the index of my C textbook – the Art and Science of C.
Anything you can share about them, especially their scope and example declaration would be helpful. I’m guessing to declare them you use ‘persistent’ as the keyword?
static void foo( void ) {
persistent unsigned int width = 5;
}
This is the only other helpful reference I could find:
“Persistent variables keep their state when the board is turned off and on, when main is run, and when system reset occurs. Persistent variables will lose their state when code is downloaded as a result of loading or unloading a file.”
http://www.newtonlabs.com/ic/ic_5.html#SEC9
thanks!
The keyword you want is
staticin local (not global) context.The context thing is important:
Here
staticmeans thatfoohas file scope (i.e. is notextern).Whereas in
lastis persistent between calls tostrtok.All that said, they are rarely used because they are rarely useful, and are totally unacceptable in a multi-threaded context (where they are a race condition waiting to happen).