9.9.1 Const Type Qualifier
The const
type qualifier is used to tell the compiler that an object is read only and must not be modified. The compiler will issue a warning or error if you attempt to modify an object declared const
in source code.
A
const
object is usually defined with initial values, as the program cannot
write to these objects at runtime. However this is not a requirement. An uninitialized
const
object is allocated space in one of the bss sections, along
with other uninitialized RAM variables, but is still treated as read-only by the
compiler.const char IOtype = ’A’; // initialized const object
const char buffer[10]; // I just reserve memory in RAM
Objects qualified
only with const
are not guaranteed to be located in program memory. The
use of some options can affect where const
-qualified object are
located. Objects qualified const
might be placed in data memory when
the -fzero-initialized-in-bss
, -fdata-sections
, or
-mno-embedded-data
options
are used. To explicitly request that the object be placed in program memory regardless
of the usage of the options listed, use the space(prog)
attribute along
with the const
qualifier (see 10.4 Variables in Program Memory).