Node:const, Next:, Previous:Constants, Up:Constants


Sometime a variable must be assigned a value once and once only; for example, it might be in read-only memory. The reserved word const is, like static and volatile, a data type qualifier that can be applied to many different data types. It declares a variable to be a constant, whose value cannot be reassigned. A const must be assigned a value when it is declared.

const double avogadro = 6.02e23;
const int moon_landing = 1969;

You can also declare constant arrays:

const int my_array[] =
{0, 1, 2, 3, 4, 5, 6, 7, 8};

Any attempt to assign a new value to a const variable will result in a compile-time error such as the following:

const.c: In function `main':
const.c:11: warning: assignment of read-only variable `avogadro'