Constants allow you to calculate the value of an expression and store it. Compare constants with preprocessor constants. Preprocessor is run before compilation of source file occurs and therefore has limited expression evaluation capabilities.
Syntax:
const name-id = value-expr;
Constants are allowed in any scope. A value-expr
must be constant:
const MyConstant = 5; // valid, constant expression
struct A
{
int a;
const double_a = a * 2; // error, value-expr is not a constant
};
You can use the following syntax to declare an array:
const name-id [ ] = { initializer-list };
Where name-id
is a name of array and initialize-list
is a comma-separated list of expressions that initialize array elements.