|
|
|
|
|
|
|
causes the compiler to reserve a memory location for someInt. On the other hand, the declaration |
|
|
|
|
|
|
|
|
is known as an external declaration. It states that someInt is a global variable located in another file and that no storage should be reserved for it here. System header files such as iostream.h contain external declarations so that user programs can access important variables defined in system files. For example, iostream.h includes declarations like these: |
|
|
|
|
|
|
|
|
extern istream cin;
extern ostream cout; |
|
|
|
|
|
|
|
|
These declarations allow you to reference cin and cout as global variables in your program, but the variable definitions are located in another file supplied by the C++ system. |
|
|
|
|
|
|
|
|
In C++ terminology, the statement |
|
|
|
|
|
|
|
|
is merely a declaration of someInt. It associates a variable name with a data type so that the compiler can perform type checking. But the statement |
|
|
|
|
|
|
|
|
is both a declaration and a definition of someInt. It is a definition because it reserves memory for someInt. In C++, you can declare a variable or a function many times, but there can be only one definition. |
|
|
|
|
|
|
|
|
Except when it's important to distinguish between declarations and definitions of variables, we'll continue to use the phrase variable declaration instead of the more specific variable definition. |
|
|
|
|
|
|
|
|
A concept related to but separate from the scope of a variable is its lifetimethe period of time during program execution when an identifier actually has memory allocated to it. We have said that storage for local variables is created (allocated) at the moment control enters a function. Then |
|
|
|
|
|