So far when we have 'declared' a variable, we have meant that we have told the compiler about the variable; i.e. its type and its name, as well as allocated a memory cell for the variable (either locally or globally). This latter action of the compiler, allocation of storage, is more properly called the definition of the variable. The stricter definition of declaration is simply to describe information 'about' the variable.
So far, we have used declarations to declare variable names and types as well as to define memory for them. Most of the time these two actions occur at the same time, that is, most declarations are definitions; however, this may not always be the case.
The prototype statement for a function declares it, i.e. tells the compiler 'about' the function - its name, return type, and number and type of its parameters. A similar statement, the function header, followed by the body of the function, defines the function - giving the details of the steps to perform the function operation.
For automatic and register variables, there is no difference between definition and declaration. The process of declaring an automatic or a register variable defines the variable name and allocates appropriate memory. However, for external variables, these two operations may occur independently. This is important because memory for a variable must be allocated only once, to ensure that access to the variable always refers to the same cell. Thus, all variables must be defined once and only once. If an external variable is to be used in a file other than the one in which it is defined, a mechanism is needed to 'connect' such a use with the uniquely defined external variable cell allocated for it. This process of connecting the references of the same external variable in different files, is called resolving the references.
As I have shown in my previous post external variables may be defined and declared with a declaration statement outside any function, with no storage class specifier. Such a declaration allocates memory for the variable. A declaration statement may also be used to simply declare a variable name with the extern storage class specifier at the beginning of the declaration. Such a declaration specifies that the variable is defined elsewhere, i.e. memory for this variable is allocated in another file. Thus, access to an external variable in a file other than the one in which it is defined is possible if it is declared with the keyword extern; no new memory is allocated. Such a declaration tells the compiler that the variable is defined elsewhere, and the code is compiled with the external variable left unresolved. The reference to the external variable is resolved during the linking process.
Here are some examples of declarations of external variables that are not definitions:
extern char stack[10];
extern int stkptr;
These declarations tell the compiler that the variables stack[] and stkptr are defined elsewhere, usually in some other file. If the keyword extern were omitted, the variables would be considered to be new ones and memory would be allocated for them. Remember, access to the same external variable defined in another file is possible only if the keyword extern is used in the declaration. Example below shows an example of a source program that references the same external variable in different files.
Example Code:
Following Files required
1. file2.c
2. file3.c
Note:
This example shows reference to an external variable in more than one
file. The program is organized in three files. The external variable a1
is defined in definition-vs-declaration.c and it is declared as extern in file3.c
Compiling and using the code,
[sgupta@rhel54x64 definition-vs-declaration]$
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c file2.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c file3.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c definition-vs-declaration.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -o definition-vs-declaration definition-vs-declaration.o file2.o file3.o
[sgupta@rhel54x64 definition-vs-declaration]$ ./definition-vs-declaration
*** Definition vs Declaration ***
a1 = 2
a1 = 13, b1 = 19.200001
a1 = 13
[sgupta@rhel54x64 definition-vs-declaration]$
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c file2.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c file3.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -c definition-vs-declaration.c
[sgupta@rhel54x64 definition-vs-declaration]$ gcc -o definition-vs-declaration definition-vs-declaration.o file2.o file3.o
[sgupta@rhel54x64 definition-vs-declaration]$ ./definition-vs-declaration
*** Definition vs Declaration ***
a1 = 2
a1 = 13, b1 = 19.200001
a1 = 13
[sgupta@rhel54x64 definition-vs-declaration]$
See Also:
No comments:
Post a Comment