externl linkage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destination
    New Member
    • Dec 2009
    • 34

    externl linkage

    "If a declaration for an identifier within a block does not include the extern specifier, then the identifier has no linkage and is unique to the function. If it does include extern, and an external declaration for is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

    this is a statement from K&R.
    Can anyone explain me this statement??kind ly give an example.
    Thanx
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    What this means is that any variable between braces {int var; } is local to that pair of braces. That is what is mean by no linkage.

    However, if the variable has extern like { extern int var; } then the variable (var) is not between the braces but is outside the braces. In this case there has to be a sharable variable (called external linkage) named var.

    But if you omit the the extern keyword, then the variablwe reverts to the first case. That is, a local variable.

    Linkage deals with whether something is shared or not between scopes { }.

    No linkage means not sharable outside the current { }.
    Static linkage means internal linkage which means sharable only in the source file that defines the variable.
    Sharable linkage means external linkage which means sharable in another source file.

    Comment

    • destination
      New Member
      • Dec 2009
      • 34

      #3
      "for is active in the scope surrounding the block"
      what does this part mean?? i had a doubt on this one.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        What section is that? I am having trouble finding your K&R quote.

        Comment

        • destination
          New Member
          • Dec 2009
          • 34

          #5
          well the quote is from my first post.posting it again.

          "If it does include extern, and an external declaration for is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

          look at the first line.what does "active in the scope " means??

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            You mis-understood, in your first post you said the quote was from K&R, weaknessforcats is asking what the section number in K&R is that contains that quote.

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              Kernighan and Ritchie: The C Programming Language, second edition, 1988, ISBN 0-13-110362-8.

              Section A11.2 Linkage, second paragraph (page 228)
              "If a declaration for an identifier within a block does not include the extern specifier, then the identifier has no linkage and is unique to the function. If it does include extern, and an external declaration for the identifier is active in the scope surrounding the block, then the identifier has the same linkage as the external declaration, and refers to the same object or function; but if no external declaration is visible, its linkage is external."

              Perhaps the words "the identifier" are missing from your edition.

              Comment

              • destination
                New Member
                • Dec 2009
                • 34

                #8
                donbock

                ya you are right the word identifier is missing in my text.
                now pls someone respond my query

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  and an external declaration for the identifier is active in the scope surrounding the block
                  Means that if the variable has already been declared externally and visibly to the block in which it has been declared then the new declaration doesn't result in a new variable but rather it uses the variable that has already been declared rather than defining a new variable which is what happens if an external declaration is not visible outside the block.

                  Comment

                  • destination
                    New Member
                    • Dec 2009
                    • 34

                    #10
                    Can u give an example so that it becomes more clear??

                    Comment

                    • donbock
                      Recognized Expert Top Contributor
                      • Mar 2008
                      • 2427

                      #11
                      You normally use extern to declare a variable that you would otherwise not have access to. That is, the extern statement is the first mention of that variable.
                      Code:
                      extern int foo;
                      foo = 2;
                      But what happens if the variable already exists in an outer scope? The Standard says that the extern declaration simply refers to that outer-scope variable, and that the linkage of this outer-scope variable is not changed by the extern statement.
                      Code:
                      extern int foo1;
                      static int foo2;
                      ...
                         {
                         int foo3;
                         ...
                            {
                            extern int foo1;   // same foo1 as from outer scope
                            extern int foo2;   // same foo2 as from outer scope
                            extern int foo3;   // same foo3 as from outer scope
                            ...
                            }
                         ...
                         }
                      Notice that the outer-scope variables were already accessible from the inner scope. The extern commands are unnecessary. I would recommend that you not write code like this. In this case, the Standard specifies necessary compiler behavior, not recommended coding style. I suppose there would be a compiler error if the extern statement declared a different type from the outer-scope definition.

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Code:
                        #include <iostream>
                        
                        static int var2 = 10;
                        
                        int main()
                        {
                            extern int var1;
                        
                            if (true)
                            {
                                extern int var2;
                        
                        	var1 += 20;
                        	var2 += 10;
                            }
                        
                            std::cout << var1 << " " << var2 << std::endl;
                        }
                        var1 is declared external and there is no declaration visible outside the block so it has external linkage.

                        var2 is declared external inside the if, however there is a declaration active outside the block which declares var2 static so the var2 inside the if refers to that same object and has the same linkage, that is internal (static) linkage.

                        This wont link (it will compile) because var1 is declared external, that implies there must be another file where var1 is defined. If you add the file
                        Code:
                        int var1 = 40;
                        
                        int var2 = 60;
                        to the project the code now compiles. The externally declared var1 in the first file links against the var1 defined in the second file. The var2 defined in the second file has no effect because the var2 in the first file is declared with internal (static) linkage.

                        If the first file did not declare var2 static at the top of the file it would have external linkage and you would get a multiply defined symbol error because var2 would be declared with external linkage in both files.

                        Comment

                        • destination
                          New Member
                          • Dec 2009
                          • 34

                          #13
                          Thanx Banfa and donbock
                          You people gave a very good explanation.

                          Comment

                          Working...