Thread: extern and global variable - different or the same?

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    80

    extern and global variable - different or the same?

    A global variable, and a variable declared outside of all functions with extern - is that the same or different?

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    test1.c:
    Code:
    #include <stdio.h>
    #include "test2.h"
    
    
    int global_var = 0;
    
    
    int main() {
        global_var = 1;
        printf("%d\n", global_var);
        test(3);
        printf("%d\n", global_var + 1);
        return 0;
    }
    test2.h:
    Code:
    extern int global_var;
    
    
    void test(int);
    test2.c:
    Code:
    #include <stdio.h>
    #include "test2.h"
    
    
    void test(int x) {
        printf("%d\n", global_var + 1);
        global_var = x;
        printf("%d\n", global_var);
    }
    outputs
    1
    2
    3
    4

    Is that the right set up of a global variable, the right use of extern? I tried deleting extern from the .h file and it made no apparent difference.

    Thanks.
    Last edited by BpB; 04-10-2017 at 12:42 AM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    A global variable has external linkage by default and when you use extern ( for globals specifically ) you're practically saying "This is just a declaration, not a definition. Expect the definition elsewhere."

    This is useful if you want your variable to be accessible through a header file and not be defined multiple times as a result of multiple translation units.

    EDIT:
    The reason you're getting no errors for removing extern in your example is because multiple declarations of globals in the same file are "ignored".
    Last edited by GReaper; 04-10-2017 at 12:47 AM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Not having extern in that test2.h file made no difference though, or does it?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by BpB View Post
    Not having extern in that test2.h file made no difference though, or does it?
    Right now it doesn't, because pure declarations are many times treated as external if the compiler feels that's appropriate.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    Doesn't make any difference I moved the global_var define from test1.c to test2.c, in the same kind of position outside functions. With and without extern in the .h file, no difference.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A variable (or more precisely, an identifier for an object) not previously declared that is declared at file scope without being declared static has external linkage, i.e., if a similiar declaration occurs in another translation unit (i.e., source file + headers), it refers to the same object.

    The issue is that some declarations of variables are also definitions, and some are tentative definitions. For example, you could do this, because of the declarations of n are tentative definitions:
    Code:
    #include <stdio.h>
    
    int n;
    int n;
    
    int main(void)
    {
        n = 123;
        printf("%d\n", n);
    }
    But you cannot do this, because the declarations of n are definitions, and an object can only be defined once:
    Code:
    #include <stdio.h>
    
    int n = 123;
    int n = 123;
    
    int main(void)
    {
        printf("%d\n", n);
    }
    Using extern (without an initialiser, and before a declaration of the identifier that declares it static and therefore having internal linkage) makes it explicit that a declaration of an identifier for an object declares the identifier to have external linkage, and that that declaration is not a definition of the object at all. Hence, it makes sense to declare global variables extern in headers because you do not want to run into the problem of including a header in multiple translation units, only to find that the declaration of the global variable is actually a definition, hence you are breaking the rule that an object can only be defined once, thus compilation (or more precisely linking) will fail. Of course, this still means you need to define the object somewhere, but you can do so in exactly one source file.
    Last edited by laserlight; 04-10-2017 at 01:42 AM.
    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

  8. #8
    Registered User
    Join Date
    Apr 2017
    Posts
    80
    I semi-understood that. I get the gist. Using extern, where appropriate rather than not, even though it's not outright required, is a good idea as it makes things explicit and avoids some possible problems. Ok that's great, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static global variable and static extern global variable
    By gunjansethi in forum C Programming
    Replies: 8
    Last Post: 01-12-2011, 01:00 AM
  2. extern Global array in seperate .h file
    By rocketman03 in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:03 AM
  3. using extern to have a global variable
    By steve1_rm in forum C Programming
    Replies: 3
    Last Post: 01-29-2009, 06:44 AM
  4. allocation for global variables in extern
    By Bobert in forum C Programming
    Replies: 13
    Last Post: 12-15-2007, 07:26 PM
  5. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 08:46 AM

Tags for this Thread