473,796 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'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 23003
Sona <so**********@n ospam.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**********@n ospam.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
2595
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. Two of the globals are pointers which are initialized at runtime. The others are classes whose values are initialized in the global space. When I had just the pointers, I didn't need to include the extern header in the defining class file. But...
2
4204
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. The problem is that I need to initialise certain variables within the first C program called and then use them in subsequent calls to other C functions.
1
2030
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 are not defined and I have no idea how this should work...sorry for the confusing email, the more I write, the worse it becomes ;-) SHORT VERSION>
3
5321
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 else.
7
9043
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: extern char muser, mpass; However, in one of these functions outside of main.c I need to snprintf into these buffers. Usually when using snprintf I call it like this: snprintf(muser, sizeof muser, "text");
18
4291
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 where it's defined.
17
4934
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
1653
by: anuragch1 | last post by:
#include <stdio.h> void main() { static int i = 1; void cal(); printf("%d\n",i); cal(); } void cal()
13
2848
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 values are determined during the execution of the initialization routines, so I made them const (I'd rather making it hard for a dev to accidentally overwrite the values). The problem is, they are being set run-time by the return of a function...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10190
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9057
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.