Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extern poses a problem for me!!!

Status
Not open for further replies.

ekanth

Programmer
Jul 28, 2001
28
0
0
US
Hai,
This doubt may seem absurd for many of you but I think it really has a point. I've always wondered with programmers' usage of the 'extern'. I just want to make clear when to and where to use extern. Sometimes programmers use extern in a file X to refer to a variable declared in a different file say Y for using the variable in the file X. But I've seen case where programmers define a variable as extern in a file where the variable is never used.
It seems like I'm completely confused with extern. Don't mind if I've interpreted anything wrong in the statements above. I want a clear explanation. Please help.

Ekanth
 
Perhaps the programmer was trying to make clear that a global variable was being used?

int iNum = -1;

void someFun()
{
extern int iNum;
//Do somthing here with global.
}

In practice the extern keyword is almost never used inside a function to access a global variable. The scope resolution operator :: is usually perfered. Maybe the programmer externed a variable and forgot to use it?

Mike L.G.
mlg400@blazemail.com
 
dear friend , i will try to explain the use of the extern with the help of the followint example.

//consider the following program.in this program i am
//defining the variable a before the function fun and
//i am using it in the main program with the help of
//the key word extern. extern tells the compiler that
//the variable (var in this case) has been defined
//somewhere else and i am trying to use it in the
//main function.

void main()
{ extern int a;
printf("you can use the variable a in the main );
}

int a=5;
void fun()
{
printf("the variable a has been defined before the fun");
}
//conclusion.
//the extern does not allocate any memory but simply
//allows the use of the variable in a function before it
//its definition.
//the variable has to be defined outside the or else it
//will a run time error.it is not a compile time error
//because when u compile the program it only tells the
//compiler that there is a variable defined else where .
//it is only at the run time that the compiler looks for
//the value and is unable to access if it is defined in
//side the fun().

naresh.
chausi@rediffmail.com


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top