The const type qualifier

The const qualifier explicitly declares a data object as something that cannot be changed. Its value is set at initialization. You cannot use const data objects in expressions requiring a modifiable lvalue. For example, a const data object cannot appear on the lefthand side of an assignment statement.

C only

A const object cannot be used in constant expressions. A global const object without an explicit storage class is considered extern by default.

End of C only
C++ only

In C++, all const declarations must have initializers, except those referencing externally defined constants. A const object can appear in a constant expression if it is an integer and it is initialized to a constant. The following example demonstrates this:

const int k = 10;
int ary[k];     /* allowed in C++, not legal in C */

In C++ a global const object without an explicit storage class is considered static by default, with internal linkage.

const int k = 12;  /* Different meanings in C and C++ */

static const int k2 = 120;  /* Same meaning in C and C++ */
extern const int k3 = 121;  /* Same meaning in C and C++ */

Because its linkage is assumed to be internal, a const object can be more easily defined in header files in C++ than in C.

End of C++ only

An item can be both const and volatile. In this case the item cannot be legitimately modified by its own program but can be modified by some asynchronous process.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]