Correct use of extern
-
I have read several sources on when to use the
extern
keyword and when not to, but I still have some confusion. Could someone explain the proper use ofextern
when it comes to variables and function:extern void MyFunction(void); extern int MyVariable;
Thanks. -
I have read several sources on when to use the
extern
keyword and when not to, but I still have some confusion. Could someone explain the proper use ofextern
when it comes to variables and function:extern void MyFunction(void); extern int MyVariable;
Thanks.masnu wrote:
Could someone explain the proper use of extern when it comes to variables and function: extern void MyFunction(void); extern int MyVariable;
In both cases, it specifies external linkage (i.e., is visible from files other than the one in which it's defined).
"The largest fire starts but with the smallest spark." - David Crow
-
I have read several sources on when to use the
extern
keyword and when not to, but I still have some confusion. Could someone explain the proper use ofextern
when it comes to variables and function:extern void MyFunction(void); extern int MyVariable;
Thanks.you use extern when you want to tell the compiler the type of a variable or function which is defined in another file. if you have a variable called blah, of type int, at global scope (not static, or local to a function) in foo.c and you want to use it in bar.c, you would put "extern int blah;" somewhere in bar.c, or in a file that bar.c includes. that way, when the compiler is working on bar.c, it knows the type of the variable 'blah'. the linker will come around later and make sure all references to 'blah' are looking at the same thing. Cleek | Image Toolkits | Thumbnail maker