If an object is not defined within a function, it belongs to the extern storage class.
Objects in this storage class have program scope and can be read and, provided they have
not been defined as const, modified at any place in the program.
External objects thus allow you to exchange information between any functions without passing any arguments.
To demonstrate this point, the program on the opposite page has been divided into two separate source files. The string line, which has a global definition, is used to exchange data.
Global objects that are not explicitly initialized during definition receive an initial
value of 0 (that is, all bits = 0) by default. This also applies to objects belonging to class
types, if not otherwise stipulated by the class.
How to Using Global Objects
An object belonging to the extern storage class is initially only available in the source
file where it was defined.
If you need to use an object before defining it or in another module, you must first declare the object. If you do not declare the object, the compiler issues a message stating that the object is unknown.
The declaration makes the name and type of the object known to the compiler. In contrast to a definition, the storage class identifier extern precedes the object name in a declaration.
Example: extern long position; // Declaration
This statement declares position as an external object of type long. The extern declaration thus allows you to “import” an object from another source file.
A global object must be defined once, and once only, in a program. However, it can
be declared as often as needed and at any position in the program.
You will normally declare the object before the first function in a source file or in a header file that you can include when needed. This makes the object available to any functions in the file.
Remember, if you declare the object within a code block, the object can only be used
within the same block.
An extern declaration only refers to an object and should therefore not be used to
initialize the object. If you do initialize the object, you are defining that object!
Global objects affect the whole program and should be used sparingly. Large programs in particular should contain no more than a few central objects defined as extern.
What is Static Objects
If an object definition is preceded by the static keyword, the object belongs to the
static storage class.
Example: static int count;
The most important characteristic of static objects is their static (or permanent) lifetime.
Static objects are not placed on the stack, but are stored in the data area of a program
just like external objects.