The key thing to remember is that const
and "constant" mean two quite different things.
The const
keyword really means "read-only". A constant is a numeric literal, such as 42
or 1.5
(or an enumeration or character constant). A constant expression is a particular kind of expression that can be evaluated at compile time, such as 2 + 2
.
So given a declaration:
const int n = 5;
the expression n
refers to the value of the object, and it's not treated as a constant expression. A typical compiler will optimize a reference to n
, replacing it by the same code it would use for a literal 5
, but that's not required -- and the rules for whether an expression is constant are determined by the language, not by the cleverness of the current compiler.
An example of the difference between const
(read-only) and constant (evaluated at compile time) is:
const size_t now = time(NULL);
The const
keyword means you're not allowed to modify the value of now
after its initialization, but the value of time(NULL)
clearly cannot be computed until run time.
So this:
const int n = 5;
int x[n];
is no more valid in C than it would be without the const
keyword.
The language could (and IMHO probably should) evaluate n
as a constant expression; it just isn't defined that way. (C++ does have such a rule; see the C++ standard or a decent reference for the gory details.)
If you want a named constant with the value 5
, the most common way is to define a macro:
#define N 5
int x[N];
Another approach is to define an enumeration constant:
enum { n = 5 };
int x[n];
Enumeration constants are constant expressions, and are always of type int
(which means this method won't work for types other than int
). And it's arguably an abuse of the enum
mechanism.
Starting with the 1999 standard, an array can be defined with a non-constant size; this is a VLA, or variable-length array. Such arrays are permitted only at block scope, and may not have initializers (since the compiler is unable to check that the initializer has the correct number of elements).
But given your original code:
const int n = 5;
int x[n] = { 1,1,3,4,5 };
you can let the compiler infer the length from the initializer:
int x[] = { 1,1,3,4,5 };
And you can then compute the length from the array's size:
const int x_len = sizeof x / sizeof x[0];