Microchip Technology SW006022-2N Data Sheet

Page of 338
Supported Data Types and Variables
 2012 Microchip Technology Inc.
DS52071B-page 103
6.9
STANDARD TYPE QUALIFIERS
Type qualifiers provide additional information regarding how an object may be used. 
The MPLAB XC16 compiler supports both ANSI C qualifiers and additional special 
qualifiers which are useful for embedded applications and which take advantage of the 
PIC MCU and dsPIC DSC architectures.
6.9.1
Const Type Qualifier
The compiler 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 
compiler will issue a warning or error.
User-defined objects declared const are placed, by default, in the program space and 
may be accessed via the program visibility space, see Section 7.4 “Variables in Pro-
gram Space”
. Us
ually 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.
The memory model -mconst-in-data will allocate const-qualified objects in data 
space, which may be writable.
6.9.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:
extern volatile unsigned int INTCON1 __attribute__((__sfr__));
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.
A C statement that consists only of a volatile variable’s name will produce code that 
reads the variable’s memory location and discards the result. For example the entire 
statement:
PORTB;
will produce assembly code the reads PORTB, but does nothing with this value. This is 
useful for some peripheral registers that require reading to reset the state of interrupt 
flags. Normally such a statement is not encoded as it has no effect.
Some variables are treated as being volatile even though they may not be qualified 
in the source code. See Chapter 13. “Mixing C and Assembly Code” if you have 
assembly code in your project.