4.3.10.8 Persistent Attribute

The persistent attribute is used to indicate that objects should not be cleared by the runtime startup code.

By default, C objects with static storage duration that are not explicitly initialized are cleared on startup. This is consistent with the definition of the C language. However, there are occasions where it is desirable for some data to be preserved across a Reset. The persistent attribute stores objects with static storage duration in a separate area of memory that is not altered by the runtime startup code.

For example, the following code ensures that the variables intvar and mode are not cleared at startup:
int __attribute__((persistent)) mode;

void test(void)
{
  static int __attribute__((persistent)) intvar;   /* must be static in this context */
  ...
}
If the CCI is enabled (see 3.6.3.4 Ext Option) and the <xc.h> header is included, a more portable macro, __persistent, is available. For example, the following CCI-compliant code is similar to the above:
#include <xc.h>
__persistent int mode;

void test(void)
{
  static __persistent int intvar;   /* must be static in this context */
  ...
}