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

Discussion in 'C' started by dream4cppexpert, Jun 28, 2012.

  1. dream4cppexpert

    dream4cppexpert New Member

    Joined:
    Jun 21, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  2. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    Re: Main difference between a variable declared globally with and without extern keyw

    ---->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!
     
  3. dream4cppexpert

    dream4cppexpert New Member

    Joined:
    Jun 21, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Re: Main difference between a variable declared globally with and without extern keyw


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

    please provide the link...
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Main difference between a variable declared globally with and without extern keyw

    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"
    
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Re: Main difference between a variable declared globally with and without extern keyw

    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"
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice