GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jul-2005, 00:43
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Question

Extern Variables and #include directive


Hi all,

I would like to have clarifications as to what is the difference between using extern declaration and #include directive in the following context:

I have 2 files (a.cc, b.cc with corresponding header files a.h and b.h)

In a.cc, i have a declared a global variable say int a =10.

To access the variable int a in b.cc, i need to preceed with the keyword extern.
Otherwise,
But when i included a.h in b.h file, i am able to access the same variable.

So what difference does this make with extern and #include directive?

Regards,
S.Karthikeyan
  #2  
Old 22-Jul-2005, 02:44
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
You are including a.h in b.h, later say you are including a.h in a.cc and b.h in b.cc and that a.cc and b.cc are part of the same project(or program) then what you are actually doing here is, you are declaring the variable 'a' twice which your complier will not like very much and will error out.

externing a variable just tell the complier that this shared variable is actually declared somewhere in other source file. So your complier will not scream about it.

Good idea (although not the best) will be to declare all the shared variable in one source file and extern it in a common header file. So that whenever the header file is included any source file all the shared variables are available in that source file.

Source file main1.c
CPP / C++ / C Code:
#include "headerfile.h"

int a=0;
int b=0;

int main(int argc, char *argv[])
{
	printf("call adda(): %d\n",adda());
	printf("call addb(): %d\n",addb());
	printf("call addab(): %d\n",addab());
	
	return 0;
}

headerfile "headerfile.h"
CPP / C++ / C Code:
#include <stdio.h>

extern int a;
extern int b;

source file adda.c
CPP / C++ / C Code:
#include "headerfile.h"

int adda ()
{
	a=a+10;
	
	return a;
}

source file addb.c

CPP / C++ / C Code:
#include "headerfile.h"

int addb()
{
	b=b+10;
	
	return b;
}

source file addab.c
CPP / C++ / C Code:
#include "headerfile.h"

int addab()
{
	return (a+b);
}

Then I complie it like

Quote:
gcc -o main1 main1.c adda.c addb.c addab.c

When I run it

Quote:
$ ./main1.exe
call adda(): 10
call addb(): 10
call addab(): 20

Thanks,
  #3  
Old 22-Jul-2005, 03:15
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Exclamation

True!!! - But this one !!


Thanks a lot. It gives a lucid explanation.

But think whether this works:

When it comes to Header files, let us say i have class definitions and have declared a global variable inside the headerfile.h;Obviously i am including this .h file in main1.cc

Now when it comes to another file headerfile1.h, i am including the headerfile.h and i have a source file to perform some function definition i have included headerfile1.h; Now i can directly access the global variable defined in headerfile.h without the use of keyword extern.

Let me know how is this possible.

Thanks
Quote:
Originally Posted by nkhambal
You are including a.h in b.h, later say you are including a.h in a.cc and b.h in b.cc and that a.cc and b.cc are part of the same project(or program) then what you are actually doing here is, you are declaring the variable 'a' twice which your complier will not like very much and will error out.

externing a variable just tell the complier that this shared variable is actually declared somewhere in other source file. So your complier will not scream about it.

Good idea (although not the best) will be to declare all the shared variable in one source file and extern it in a common header file. So that whenever the header file is included any source file all the shared variables are available in that source file.

Source file main1.c
CPP / C++ / C Code:
#include "headerfile.h"

int a=0;
int b=0;

int main(int argc, char *argv[])
{
	printf("call adda(): %d\n",adda());
	printf("call addb(): %d\n",addb());
	printf("call addab(): %d\n",addab());
	
	return 0;
}

headerfile "headerfile.h"
CPP / C++ / C Code:
#include <stdio.h>

extern int a;
extern int b;

source file adda.c
CPP / C++ / C Code:
#include "headerfile.h"

int adda ()
{
	a=a+10;
	
	return a;
}

source file addb.c

CPP / C++ / C Code:
#include "headerfile.h"

int addb()
{
	b=b+10;
	
	return b;
}

source file addab.c
CPP / C++ / C Code:
#include "headerfile.h"

int addab()
{
	return (a+b);
}

Then I complie it like



When I run it



Thanks,
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 07:10
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:53.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.