Extern

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Time
    New Member
    • Jan 2010
    • 77

    Extern

    hi,
    Every one here knows the difference between global variables and extern variables.
    Thing am not sure about is; if i have defined a global variable outside main(); before main, it works fine.
    if we define it at the same place and use extern in main(), it still works.
    It is totally right syntactically and oterwise also.
    But isn't it a bit weird?.. i mean it's not defined outside the file and still the use of extern is not raising any error?
    I feel that it should raise an error.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A code example to demonstrate your point would have been good but I assume you mean something like

    Code:
    int example = 0;
    
    int main()
    {
        extern int example;
    
        example = 5;
    
        return 0;
    }
    So what you are probably missing is that the compiler works on a single source (cpp) file and and files it includes. It does not consider multiple source files at a time so when you compile a file it does not consider the contents of any other file.

    Also you seem to believe that an extern statement indicates that a symbol (variable or function) exists in another file, however what extern really means is that a symbol exists somewhere, I'm not saying where.

    Having an extern declaration in the same compilation unit (source file plus all the files it includes) as a definition for the same symbol is usual as that ensures your definition and extern declaration match. Normally the extern declaration would be in a header for inclusion into multiple source files.

    So in the above code line 1 defines the symbol example then line 5 declares it as existing somewhere, which is true it exists in this file. There is no syntactic or logic error. Line 5 is superfluous in this instance since definitions are also declarations but it is not breaking an rules that require a warning or error. You can declare a symbol as many times as you want but it must be defined once and only once with-in its scope.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The extern keyword has to do with linkage. That is, whether or not the variable is sharable outside the current scope.

      A static global variable is not sharable outside the current scope, typically the current file. It is accessible only from the point of definition to the end of the file.

      An extern variable is sharable outside the current scope. In the case of the exanple variable, the variable is defined and a value placed in it. The variable is sharable (accessible) from other source files:

      Code:
      extern int example = 0;
      However, if there is no value, then the variable does not need to bed defined. In this case, the extern means there is a sharable variable somewhere else:

      Code:
      extern int example;  //no variable created
      const global variables have internal linkage (static) and so are not sharable with other source files. However, this is the default behavior. You can create a sharable const variable by defining using the extern keyword:

      Code:
      extern const int example = 0;
      and this variable is accessible from another source file by:

      Code:
      extern const int example;

      Comment

      • Time
        New Member
        • Jan 2010
        • 77

        #4
        It was helpful enough.
        Thanks to both of you..:)

        Comment

        Working...