is extern int i - a declaration or defination?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shraddha18
    New Member
    • Sep 2013
    • 1

    is extern int i - a declaration or defination?

    is the following statement a declaration or defination ?
    extern int i
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It's nothing it has not ending semi-colon however

    Code:
    extern int i;
    Is a declaration, it declares that somewhere in the program a variable called i with type int exists at global scope. It does not define the variable though, that is it does not allocate memory to contain the variable.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Depends.

      Code:
      extern int i;
      is an assertion that there is an int named i defined in another file .c with external linkage. External linkage means accessible from outside the file such as a global variable. So in this case this is a declaration. You will need to define the int in some other .c file that is part of your build.

      On the other hand:

      Code:
      extern int i = 0;
      Is a definition since the compiler needs to create an int to contain that zero.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        It depends on the C implementation.
        In general, compilers use one of these four models for top-level variables.

        The Initializer Model...
        • Exactly one of the declarations has an initializer. That is the definition and the others are references.
        • The extern keyword is assumed if it is not present.


        The Omitted Storage Class Model...
        • Exactly one of the declarations must omit the extern keyword. That is the definition and all of the others are references.
        • Only the definition is allowed to have an [optional] initializer.


        The Common Model...
        • Same as The Initializer Model except the initializer is optional.
        • If there is no initializer, then the linker chooses one instance to be the definition. The rest are references.


        The Mixed Common Model...
        • Same as The Common Model except the optional initializer can only be present with a declaration that does not have the extern keyword.

        I don't know if any of this applies to C++.
        Last edited by donbock; Sep 12 '13, 02:42 PM. Reason: Added C++ caveat.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Just when I thought I knew how this worked.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Yet another reason why global variables are a bad idea.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              I tend not to use global variables any more; but that wasn't always the case. I recall developing the following idiom some years ago to insure that the definitions and declarations are consistent.

              Create a header file with all the global variables:
              Code:
              #ifdef DEFINITIONS
              #define GLOBAL(var,val) var = val
              #else
              #define GLOBAL(var,val) extern var
              #endif
              
              GLOBAL(v1,0);
              GLOBAL(v2,10);
              ...
              GLOBAL(v99,0);
              Then #include the global-variable header in every source file that needs them. However, in one of those source files I would #define DEFINITIONS before including the header. Notice that the macro requires an initial value for every global variable.

              What's good about this: the code (after macro expansion) is compatible with all four models. (But you could do the same manually.)

              What's very bad about this: if I don't define DEFINITIONS in any source file and if my compiler uses The Common Model or the Mixed Common Model then I get no warnings but none of my global variables are initialized!

              Comment

              Working...