Main difference between a variable declared globally with and without extern keyword?

dream4cppexpert's Avatar, Join Date: Jun 2012
Newbie Member
Folks,

This may be simply and very old question to some of you, but please let me know the Basic or Main Difference between a variable declared without "extern" keyword and with "extern" keyword??????

Here is the scenario:
1. ------> I have declared a variable int glo_var; in Test.h and including Test.h in different files. ex: Test1.c, Test2.c, Test3.c and i could able to Read/Modify the variable glo_var without any problem.

2. -------> I have declared a variable extern int glo_var; in Test.h and including Test.h in different files. ex: Test1.c, Test2.c, Test3.c and i could able to Read/Modify the variable glo_var without any problem.

I had gone through the difference websites to know about this, but i couldnt get main difference, please let me know the difference as much as detail.

Thanks in advance.
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
---->extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.


Please read the post in the link below. Nice, clear and intuitive!
dream4cppexpert's Avatar, Join Date: Jun 2012
Newbie Member
Quote:
Originally Posted by dearvivekkumar View Post
---->extern keyword extends the visibility of the C variables and C functions. Probably that’s is the reason why it was named as extern.


Please read the post in the link below. Nice, clear and intuitive!

------------------

please provide the link...
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
It's actually quite simple. If you define a variable in a source file (suppose you define int foobar; in Test1.c), outside the scope of any function, then that variable is not local to any function but global to the module. It is accessible by all functions in Test1.c, but it is not by default accessible anywhere else (in Test2.c or Test3.c).

If you want access to it in Test2.c or Test3.c then you have to redefine it, but if you define it in exactly the same way (int foobar; ) then you will get *another* foobar, and a linker error due to a redefined symbol.

So in Test2.c and Test3.c you have to define it as "extern int foobar;" - then the compiler knows not to create a new foobar but just to assume it already exists and let the linker resolve the address.

Now, you appear to be saying that if you *only* define a variable as "extern int glo_Var;" it compiles fine and you get access to it everywhere. This appears to be compiler-dependent; in Visual Studio 2010 if I try this I get an "undefined symbol" error. One of those definitions must actually define glo_Var - you have to miss off the "extern" in one place.

This can make header files slightly tricky; either you have to define variables twice (duplication means you have to maintain both copies of the variable definition, which can lead to bugs if you don't), or you have to muck about with a conditional definition and define the variables as, for example:
Code:
EXTERN int glo_Var;
then for one module, define EXTERN as nothing, and for all other modules define EXTERN as extern, i.e.:
Code:
variable_defs.h:
EXTERN int glo_Var;

Test1.c:
#define EXTERN
#include "variable_defs.h"

Test2.c:
#define EXTERN extern
#include "variable_defs.h"
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Or for even less coding:
Code:
variable_defs.h:
#ifndef EXTERN
#define EXTERN extern
#endif
EXTERN int glo_Var;

Test1.c:
#define EXTERN
#include "variable_defs.h"

Test2.c:
#include "variable_defs.h"