'extern'ing variables

S

Sona

Hi,

I read some place that if you have two or more modules or files in C,
you can share variables between the these modules or files using the
'extern' keyword. I'm not really sure how to do this and when to do
this. For example, if main.c has:

double some_val; // global

and first.c wants to use the variable and the value that was assigned to
it in a function that main calls, we should do:

extern double some_val; //global

in the file.. or better yet, we should do this in the header file. Is
this correct? When we're declaring this variable in two files, how does
the compiler know not to really 'declare' the variable since it'll be
visible by another file after the linking process.

Could someone explain the use of this keyword to me please, and advise
me when it's right to use it? Thanks

Sona
 
K

Kevin Easton

Sona said:
Hi,

I read some place that if you have two or more modules or files in C,
you can share variables between the these modules or files using the
'extern' keyword. I'm not really sure how to do this and when to do
this. For example, if main.c has:

double some_val; // global

and first.c wants to use the variable and the value that was assigned to
it in a function that main calls, we should do:

extern double some_val; //global

in the file.. or better yet, we should do this in the header file. Is
this correct? When we're declaring this variable in two files, how does
the compiler know not to really 'declare' the variable since it'll be
visible by another file after the linking process.

Yes, that's correct. The compiler knows not to reserve storage for the
object ("define" it) because "extern double some_val;" is a declaration,
not a definition. Definitions reserver storage; declarations do not,
they merely inform the compiler of the name and type of some object.
Could someone explain the use of this keyword to me please, and advise
me when it's right to use it? Thanks

Usual practice is to put:

extern int foo;

into foo.h, and put:

#include "foo.h"
int foo = 0;

into foo.c, and put:

#include "foo.h"

into all the other .c files that need to access the variable "foo".
Including foo.h in foo.c allows the compiler to check the declaration in
foo.h against the corresponding definition in foo.c to ensure that they
match.

- Kevin.
 
D

Deepak

Yes , the C support this feature. The extern keyword directs the
compiler to treat the variable as it is declared in the scope of that
file. Although extern does not allocate or reserv the space for that
variable but compiler will not warn you for the references that are
made in the file in which it is declared extern.

But this extern variable must be defined in global scope in some other
file which will allocate space for the varible.
At the linking time, linker will resolve all the references to extern
variable to that which is defined in some source file.

And if that extern variable is not defined the linker will give the
error and will exit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top