1

Possible Duplicate:
What is external linkage and internal linkage in C++

Actually I want to know the importance of extern.

First I wrote some code :

file1.h

extern int i;

file2.c

#include<stdio.h>
#include "file1.h"
 int i=20;
int main()
{
   printf("%d",i);
   return 0;
}

Now my question is that: what is the use of extern variable when I have to define the i in file2.c, declaring in file1.h is in which way useful to me .

sudhanshu

Community
  • 1
  • 1
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74

7 Answers7

5

extern allows you to declare a variable (notify the compiler of its existence) without defining it (bringing it into existence). The general rule is that you can declare things as many times as you want, but you can only define them once.

This is useful if, for example, it will be defined elsewhere.

Consider:

file1.c:                   file2.c:
    extern int xyzzy;          int xyzzy;
    void fn (void) {
        xyzzy = 42;
    }

When you link together those two functions, there will be one xyzzy, the one defined in file2.c, and that's the one that fn will change.

Now that happens even without the extern. What the extern does is to declare that xyzzy exists to file1.c so that you don't get any I don't know what the blazes "xyzzy" is errors.

In your particular case (if those are your only two files), you don't need it. It's only needed if you had another file that #included file1.h and tried to access i.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

It's useful when you have three files:

foo.h

extern int i;
void bar();

a.c:

#include "foo.h"
int i = 6;
int main(){
    bar();
}

b.c:

#include "foo.h"
void bar(){
      printf("%d\n", i);
}

As you can see, i is shared between both a.c and b.c, not redefined per-file, as would happen without the extern keyword.

Dave
  • 10,964
  • 3
  • 32
  • 54
3

The "extern" declaration in C is to indicate that there is an existence of, and the type of, a global variable.

In C mostly each .c file behaves like a seperate module. Hence a variable with "extern" is something that is defined externally to the current module.

It is always a better practise to define the global in one place, and then declare extern references to it in all the other places. When refering to globals provided by any shared library, this is important in order to make sure that your code is referring the correct and common instance of the variable.

Hiren
  • 341
  • 1
  • 4
  • 17
3

From Wikipedia:

When a variable is defined, the compiler allocates memory for that variable and possibly also initializes its contents to some value. When a variable is declared, the compiler requires that the variable be defined elsewhere. The declaration informs the compiler that a variable by that name and type exists, but the compiler need not allocate memory for it since it is allocated elsewhere.

Extern is a way to explicitly declare a variable, or to force a declaration without a definition

If a variable is defined in file 1, in order to utilize the same variable in another file, it must be declared. Regardless of the number of files, this variable is only defined once, however, it must be declared in any file outside of the one containing the definition.

If the program is in several source files, and a variable is defined in file1 and used in file2 and file3, then extern declarations are needed in file2 and file3 to connect the occurrences of the variable.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
pragati
  • 95
  • 2
  • 9
1

Having extern tells the compiler its a declaration of a variable.

In your example in file1.h imagine what would happen if you didn't specify extern keyword. It will look like there are two definitions of int i. Also, a header file can be included in many .c files. If you compile those .c files and link them, linker would see multiple definitions of the same variable.

Manohar
  • 3,865
  • 11
  • 41
  • 56
1

extern keyword allows you to say to the compiler ... " Use this variable right now .. I will define and initialize it later ".. i.e " Compile it now .. I will link to its definition later".

You can extern the variable when you define it in some other file and you want to use it in the current context..

Anerudhan Gopal
  • 379
  • 4
  • 13
1

The declaration in the header allows you to access the variable from more than one source file, while still only defining it in one place.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644