In C, a const always occupies storage and its name is global.
This is nonsense, though I'd like to see more of the context.
First off, const
is a a type qualifier, not a kind of entity. It means "read-only", not "constant" (something is "constant" if it can be evaluated at compile time). Referring to something as "a const" is like calling it "a read-only".
The author probably means something like "an object of some const
-qualified type".
For example, given
const int n = 42;
you might say that n
is "a const".
In the abstract machine, any object, const
or not, occupies storage. It has an address, and that address is unchanged throughout the object's lifetime. You can evaluate &n
and it will yield a pointer value of type const int*
.
However, a compiler is free to omit the storage for any object if that storage is not actually used. Though n
is not a constant expression in C, it's perfectly legal for a compiler to replace each occurrence of n
in an expression by the constant value 42
. If it can prove that no use of n
depends on it being stored somewhere, it can optimize away the storage that would otherwise have been used to hold it.
As for its name being "global", that's also nonsense. The C standard doesn't use the term "global". An identifier has a scope, which defines the region of program text in which the name is visible. If n
is defined inside a function, the name is visible only within the enclosing block. If it's defined outside any function, the name has file scope, and its name is visible from its definition to the end of the source file.
The const
type qualifier doesn't affect either the lifetime of the object or the scope of its name.
The author is probably trying to show a difference between C and C++. There is such a difference, but it's not what the author says it is. In C, given
const int n = 42;
the name n
, when used in an expression, is not a constant expression. That means, for example, it can't be used in a case label. (This applies only if the initial value is itself a constant expression.) In C++, the name n
is a constant expression. However, C++ still permits you to take the address of n
.