Thread: Linkage vs scope of a variable

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Question Linkage vs scope of a variable

    Let's say we have defined variable i as:

    int i;

    So this variable is said to have file scope and external linkage - meaning that it is visible to the end of the file in which it was defined (file scope) and can be accessed by other files (external linkage). Isn;t that just a contradiction?!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    What is the contradiction?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by john.c View Post
    What is the contradiction?
    Well I think it is obvious - How can it be accessed by other file if it has file scope in the same time?

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    It makes some sense if you just understand "file scope" to mean not limited ​to a block or function.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    Well I think it is obvious - How can it be accessed by other file if it has file scope in the same time?
    Consider this program consisting of two source files:
    Code:
    /* main.c */
    
    #include <stdio.h>
    
    void foo(void);
    
    void bar(void)
    {
        extern int i;
        printf("i=%d\n", i);
    }
    
    void baz(void)
    {
        printf("i is equal to %d\n", i);
    }
    
    int main(void)
    {
        foo();
        bar();
        baz();
    }
    Code:
    /* foo.c */
    
    int i;
    
    void foo(void)
    {
        i = 123;
    }
    If you compiled the above program, you would find that the compiler calls you out on an error in main.c concerning the baz function, i.e., it would complain about i not being declared or something like that.

    The reason is that the name i has file scope in foo.c, but in main.c its scope is that of the bar function, hence it is out of scope in the baz function. Yet, because i has external linkage, the i that was declared extern in bar refers to the same int object as the i that was declared at file scope in foo.c
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Thanks for explanation guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of a variable
    By Satya in forum C Programming
    Replies: 3
    Last Post: 05-21-2014, 07:54 AM
  2. Replies: 8
    Last Post: 02-14-2010, 04:14 PM
  3. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  4. Variable Scope
    By tinkerbell20 in forum C++ Programming
    Replies: 5
    Last Post: 06-22-2005, 10:31 PM
  5. Get sense of internal linkage and external linkage
    By gandalf_bar in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 05:57 AM

Tags for this Thread