constconst is used with a datatype declaration or definition
to specify an unchanging value
const int five = 5; const double pi = 3.141593;
const objects may not be changed
const int five = 5; const double pi = 3.141593; pi = 3.2; five = 6;
volatilevolatile specifies a variable whose value may be changed
by processes outside the current program
volatile object might be a buffer used
to exchange data with an external device:
int
check_iobuf(void)
{
volatile int iobuf;
int val;
while (iobuf == 0) {
}
val = iobuf;
iobuf = 0;
return(val);
}
if iobuf had not been declared volatile,
the compiler would notice that nothing happens inside the
loop and thus eliminate the loop
const and volatile can be used together
const volatile (or volatile const, order is
not important) to make sure the compiler knows that the variable should
not be changed (because it is input-only) and that its value may be altered
by processes other than the current program