next up previous contents
Next: 5.4 Constants Up: 5.3 Local and Global Previous: 5.3.1 Variable Initialization

5.3.2 Persistent Global Variables


A special uninitialized form of global variable, called the ``persistent'' type, has been implemented for IC . A persistent global is not initialized upon the conditions listed for normal global variables.

To make a persistent global variable, prefix the type specifier with the key word persistent. For example, the statement

persistent int i;

creates a global integer called i. The initial value for a persistent variable is arbitrary; it depends on the contents of RAM that were assigned to it. Initial values for persistent variables cannot be specified in their declaration statement.

Persistent variables keep their state when the Handy Board is turned off and on, when main is run, and when system reset occurs. Persistent variables, in general, will lose their state when a new program is downloaded. However, it is possible to prevent this from occurring. If persistent variables are declared at the beginning of the code, before any function or non-persistent globals, they will be re-assigned to the same location in memory when the code is re-compiled, and thus their values will be preserved over multiple downloads.

If the program is divided into multiple files and it is desired to preserve the values of persistent variables, then all of the persistent variables should be declared in one particular file and that file should be placed first in the load ordering of the files.

Persistent variables were created with two applications in mind:

Fred G. Martin
Fri Mar 29 17:44:15 EST 1996