473,395 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

'extern'ing variables

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
2 22946
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John Ratliff | last post by:
I have a few global variables in my app. They are defined in the main application class file and declared (extern) in a header which can be included by anyone who would want to use these variables....
2
by: Paul M | last post by:
Hi, This is on an AS/400 which can be a little strange but I think the basic question is portable. I have a (non-C) program that needs to make series of calls to some C programs/functions....
1
by: smackdab | last post by:
Hi, I am using MSVC to compile GD and I think I am down to my last problem (we'll see ;-) I can compile libgd.dll and libgd.lib ok but a gdtest.exe program that refers to extern font variables...
3
by: j0mbolar | last post by:
void myfunction(void) { extern int myvariable; return ; } what is the point in allowing local variables to have external linkage? its scope is only myfunction so it can't be used anywhere...
7
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this:...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
2
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
13
by: S James S Stapleton | last post by:
I have some code, and I want to make it future-resistant. I have a bunch of variables that are set up run-time, and once set up, should act as constants. I don't want to #define them, because their...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.