06-24-2010 09:08 PM
"global" is a slang term, so to speak.
"top level declaration" is the term of art used to generally describe a declaration that is of the largest possible scope for a given language implementation.
Sometimes this is a "package" or, in the case of C, a compilation unit/file/module. So in C it's any declaration that's outside of a block. C is notorious for not defining how these top-level declarations are known to be either a defining declaration or a referencing declaration, and if the declaration is shared with other modules or not.
H&K has a good discussion of this. It's an important but often poorly understood topic in C.
For CVI it turns out that any top level declaration not labeled as static is shared amongst all modules in the project, though each module must separately "declare" it as either a reference (using "extern" for example) or a definintion ("extern" not used, or it's initialized, or just declared). So if you have two modules that each have a top level declaration of a variable "foo" the linker will cause these to be shared.
C89 and C99 both require top level variables, if they are initialized, to have a constant initializer. This may seem like a strange constraint, but then C has been around a long time.
NotANumber isn't executed until runtime.
But a top level variable must be allocated and intialized when the program is first loaded into memory.
C99 accomodates this by providing a standard FP_NAN constant.
so I can have a C module in a confirming C99 implementation:
#include math.h
double dDouble = FP_NAN;
int foo (void) {
double dAnotherDouble = NotANumber();
if ((isnan (dDouble) && isnan(dAnotherDouble)) {
// Will be true since both the top level and automatic block scope doubles are initialized to NaN, one statically, and one dynamically.
}
} // end, foo
Menchar