Borland C++ Builder: The Complete Reference



Variables of type const may not be changed during execution by your program.
For example,
const int a;
will create an integer variable called a that cannot be modified by your program. It can,
however, be used in other types of expressions. A const variable will receive its value
either from an explicit initialization or by some hardware-dependent means. For
example, this gives count the value of 100:

const int count = 100;
Aside from initialization, no const variable can be modified by your program.
The modifier volatile is used to tell the compiler that a variable's value can be
changed in ways not explicitly specified by the program. For example, a global
variable's address can be passed to the clock routine of the operating system and used
to hold the time of the system. In this situation, the contents of the variable are altered
without any explicit assignment statements in the program. This is important because
C automatically optimizes certain expressions by making the assumption that the
content of a variable is unchanging inside that expression. Also, some optimizations
may change the order of evaluation of an expression during the compilation process.
The volatile modifier prevents these changes from occurring.

It is possible to use const and volatile together. For example, if 0x30 is assumed to
be the address of a port that is changed by external conditions only, then the following
declaration is precisely what you would want to prevent any possibility of accidental
side effects:

const volatile unsigned char *port = (const volatile char *) 0x30;
Declaration of Variables
As you probably know, a variable is a named location in memory that is used to hold a
value that can be modified by the program. All variables must be declared before they
are used. The general form of a declaration is shown here:

type variable_list;
C h a p t e r 2 :
V a r i a b l e s , C o n s t a n t s , O p e r a t o r s , a n d E x p r e s s i o n s
19
THE
FOUNDATION
OF