C
Chris Torek
I nearly replied in a similar fashion last night, but then I realized
that it *may* actually be valid (e.g. if used in a function scope) :-(
As someone pointed out recently, you cannot guess wrong all the time.
It *is* valid when in a function, as you note.
On the other hand, it is *not* a constant, as can be seen
by referring to it in a "case" statement, for instance:
void f(int i, int j) {
const int k = min(i, j);
switch (i) {
case k:
puts("i was the min (or equal to j -- degenerate case)");
break;
default:
puts("j was the min");
break;
}
}
Moreover, readonly variables in C cannot be used to dimension
non-VLAs (VLAs being new in C99):
% cat foo.c
const int N = 20;
int a[N];
is illegal in both C89 and C99, and replacing the second line with:
void f(void) {
int b[N];
}
is valid only in C99 (at which point "sizeof b" requires a runtime
evaluation of sizeof, in the abstract machine, though a good compiler
can still optimize it away).
(C++ is, in my opinion, far better than C here. C should have
spelled the keyword "readonly", since it never makes constants.
But then, C has a tradition of misspelling keywords, such as
"typedef" for "do not define a type". )