Microchip Technology XC8 Standard Compiler (Workstation) SW006021-1 SW006021-1 User Manual

Product codes
SW006021-1
Page of 518
C Language Features
 2012 Microchip Technology Inc.
DS52053B-page 161
5.4.7
Standard Type Qualifiers
Type qualifiers provide additional information regarding how an object may be used. 
The MPLAB XC8 compiler supports both ANSI C qualifiers and additional special qual-
ifiers which are useful for embedded applications and which take advantage of the 8-bit 
PIC MCU architecture.
5.4.7.1
CONST TYPE QUALIFIER
MPLAB XC8 supports the use of the ANSI type qualifiers const and volatile.
The const type qualifier is used to tell the compiler that an object is read only and will 
not be modified. If any attempt is made to modify an object declared const, the com-
piler will issue a warning or error.
User-defined objects declared const are placed in a special psect linked into the pro-
gram space. Objects qualified const may be absolute. The @ address construct is 
used to place the object at the specified address in program memory as in the following 
example which places the object tableDef at address 0x100.
const int tableDef[] @ 0x100 = { 0, 1, 2, 3, 4};
Usually a const object must be initialized when it is declared, as it cannot be assigned 
a value at any point at runtime. For example:
const int  version = 3;
will define version as being an int variable that will be placed in the program mem-
ory, will always contain the value 3, and which can never be modified by the program. 
However, uninitialized const objects can be defined and are useful if you need to place 
an object in program memory over the top of other objects at a particular location. Usu-
ally uninitialized const objects will be defined as absolute as in the following example.
const char checksumRange[0x100] @ 0x800;
will define the object checksumRange as a 0x100 byte array of characters located at 
address 0x800 in program memory. This definition will not place any data in the HEX 
file.
5.4.7.2
VOLATILE TYPE QUALIFIER
The volatile type qualifier is used to tell the compiler that an object cannot be guar-
anteed to retain its value between successive accesses. This prevents the optimizer 
from eliminating apparently redundant references to objects declared volatile 
because it may alter the behavior of the program to do so.
Any SFR which can be modified by hardware or which drives hardware is qualified as 
volatile
, and any variables which may be modified by interrupt routines should use 
this qualifier as well. For example:
volatile static unsigned int  TACTL @ 0x160;
The volatile qualifier does not guarantee that any access will be atomic, which is 
often not the case with the 8-bit PIC MCU architecture. All these devices can only 
access a maximum of 1 byte of data per instruction.
The code produced by the compiler to access volatile objects may be different to 
that to access ordinary variables, and typically the code will be longer and slower for 
volatile
 objects, so only use this qualifier if it is necessary. However, failure to use 
this qualifier when it is required may lead to code failure.
Another use of the volatile keyword is to prevent variables being removed if they 
are not used in the C source. If a non-volatile variable is never used, or used in a 
way that has no effect on the program’s function, then it may be removed before code 
is generated by the compiler.