By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,918 Members | 2,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,918 IT Pros & Developers. It's quick & easy.

'extern'ing variables

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sona <so**********@nospam.com> wrote:
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.

Nov 13 '05 #2

P: n/a
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.
--------------------------------------------------
Sona <so**********@nospam.com> wrote in message news:<3f********@clarion.carno.net.au>...
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

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.