tags:

views:

73

answers:

2

Still confused with declaration and definition in term of C: if a header file is like:

#ifndef _BASIC_H_
#define _BASIC_H_

void test();
extern int i; //define or declare
#endif

and two source file f1.c and f2.c contain this header, then one source file need to define the variable "i".

but if the header file is like:

#ifndef _BASIC_H_
#define _BASIC_H_

void test();
int i; //define or declare
#endif

and two source files f1.c and f2.c, contain this header without define the variable "i" in any file, it still goes through when I use the variable.
my questions is when the variable is defined.

Thanks

+7  A: 

Defining a variable is when you allocate memory for the storage and maybe assign it a value. Declaring is when you state that a variable with a specific name and type exist, but memory has been allocated for it already.

The use of the extern keyword means that you are declaring the variable but not defining it.

In terms of your specific question, your first example is declaring and your second answer is defining.

Thomas Owens
+1 for concise answer
ChrisBD
I pretty much just summarized The C Programming Language's material on the matter. Thanks, though.
Thomas Owens
Note that '`int i;`' gives a definition of the variable. The strict reading of the C standard says that if more than one file includes that header, then you will get multiple definitions for `i`, and the link phase of the compilation will fail. Appendix J (§J.5.11), gives a 'common extension' as: **Multiple external definitions**There may be more than one external definition for the identifier of an object, with orwithout the explicit use of the keyword `extern`; if the definitions disagree, or more thanone is initialized, the behavior is undefined (6.9.2).
Jonathan Leffler
A: 

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: Because memory for a variable must be allocated only once, to ensure that access to the variable always refers to the same cell. 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.

It 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.

Ex.
Code:

    //file1.c
    extern char stack[10];    
    extern int stkptr;
    ....

    //file2.c    
    char stack[10];    
    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.

www
[Nice cut and paste job there.](http://ee.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.4.html)
Mark Rushakoff
Three out of four answers he's given are attribution-less cut and paste jobs. I put him in the corner.
Will