I need to use a constant array, Since the array is accessed by several functions it ended up in global space.
What you're calling "global space" is formally known as
top-level scope. This is a scope that is visible from the point of declaration to the end of the file. As mentioned above,
const is a type qualifier and is not related to the scope in which an identifier is visible.
const restricts what can be done to the object, not where it can be seen.
Along with scope is the issue of
extent, the time during which the object is defined. Objects can have either local or static extent, which is related to the place they are allocated in memory. Local extent objects are called
automatic and are allocated on the stack, in the current stack frame. Each time the block is executed, they are initialized in the top frame of the stack. When their enclosing block is exited, they are deallocated as the top frame of the stack is discarded. Static extent objects, by contrast, are allocated in a permanent location (called the Data segment) and remain there for the entire program execution. All top-level declarations have static extent, as do local (block) declarations with the
static storage class specifier.
A different, related, issue is linker visibility. The object that you declare in top-level scope can be truly local to its own file, or made visible ("exported") to the linker so it can be seen in other files, as well. When the storage class
static is used on the top-level declaration, it ensures that only its own file can see it. If
static is not used, the storage class defaults to
extern which is made visible to other files via the linker. The only way that
extern is literally typed in is for declarations that
refer to a definition in another file. So the three cases for linker visibility are:
- nothing - external and defined by this file
- extern - external and defined in another file
- static - internal
Under what conditions should constants be declared in global space?
Constants should be defined with static extent, as that ensures they are initialized at load time only. Both top-level declarations, and local declarations with
static have static extent. The choice depends on whether the object must be visible in more than one function's scope.
If the constant is declared in main or in a function does it need to be declared as static?
See above—declaring an object as
static ensures it has static extent.
Execution Speed is a concern, is there a benefit between local vs global scope?
For maximum execution speed, compile with optimizations; this will often inline the constant values in the instructions so they do not require memory accesses. Memory for local vs global scope are accessed at the same speed on most machine architectures.