Thread: About the Extern specifier...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    About the Extern specifier...

    So, i'm reading about the Extern specifier and there's a couple of things i'm not quite getting and i thought maybe someone here could help me

    The book i'm reading states that "we declare a global variable in file A, and then declare the variable again using the extern specifier in file B"

    So, in file A i'd place

    int i;

    and in file B

    extern int i; ??


    The most confusing part is "You can ignore the extern specifier, but include an
    initializer, when you declare a global variable."

    so if i initialize a global variable like such

    int i = 10;

    does this mean i no longer need extern to make it visible on other source files?


    "You should use the extern specifier (without an initializer) when you allude to a
    global variable defined elsewhere."

    Does this mean that i should not initialize the global variables but
    rather make them extern and initialize them in the individual source files?

    Sorry if the question seems dumb... but, i'm kind of a noob so :P

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You do need to declare the variable in any source file it is used. That way the compiler knows how to process it, what type it is.
    The initializer, if any, is only to be used in the source unit that does not have 'extern'.
    It's a good idea to include 'extern' in all source units other than the primary one so that people can understand that its declaration (and initializer value) is found elsewhere. But strictly speaking, 'extern' is optional. You may only specify the initializing value in one file or else the compiler/linker will yell at you.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Jony View Post
    So, i'm reading about the Extern specifier and there's a couple of things i'm not quite getting and i thought maybe someone here could help me

    The book i'm reading states that "we declare a global variable in file A, and then declare the variable again using the extern specifier in file B"

    So, in file A i'd place

    int i;

    and in file B

    extern int i; ??
    Yes, that is the general idea. Though you should note that global variables are generally considered bad practice. Read this link: Global Variables Are Bad.

    The most confusing part is "You can ignore the extern specifier, but include an
    initializer, when you declare a global variable."
    Ignoring the extern specifier and including an initializer means you are defining a global variable. There's a difference. Definition is where the variable is created and space is set aside for it. This should exist in exactly one .c file, and never in a header (.h) file. Declaration is simply stating the type of the variable, e.g. it's an int or float, char array, pointer to struct foo, etc. Declarations simply allow the compiler to check and make sure you're using that variable correctly.

    so if i initialize a global variable like such

    int i = 10;

    does this mean i no longer need extern to make it visible on other source files?
    No. If you put "int i = 10;" in all of your .c files, and never use extern, you end up with a separate variable for each .c file. Changing i in one file won't change it in any of the others. Your global variables will get out of sync.

    "You should use the extern specifier (without an initializer) when you allude to a
    global variable defined elsewhere."

    Does this mean that i should not initialize the global variables but
    rather make them extern and initialize them in the individual source files?
    No. It means you define the variable once, in exactly one .c file, and declare it extern in every other .c file that needs to use that global variable. Normally, this is made easier by creating a header file that declares the extern symbols for you. Whether you initialize a global variable depends on whether you want it to have a specific value when the program starts, or just be zero (uninitialized globals are always set to zero).

    Sorry if the question seems dumb... but, i'm kind of a noob so :P
    Don't apologize. We were all there at one point, and you've done nothing wrong (other than use what sounds like a crappy book).

    Here's a small example of how to use extern:
    Code:
    // foo.h
    #ifndef foo_h__  // I'm an include guard!  Use me in every header file.  Read about me here: http://en.wikipedia.org/wiki/Include_guard
    #define foo_h__
    
    
    extern int foo;
    
    
    #endif // foo_h__
    
    
    // foo.c
    int foo;  // here is where I define the variable
    
    
    // bar.c
    #include "foo.h"
    
    
    void bar(void)
    {
        printf("foo = %d\n", foo);
    }
    Last edited by anduril462; 08-26-2011 at 11:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Specifier
    By Nextstopearth in forum C Programming
    Replies: 4
    Last Post: 09-12-2008, 08:31 PM
  2. got confused which specifier to use ?!
    By Masterx in forum C Programming
    Replies: 12
    Last Post: 12-22-2007, 08:27 PM
  3. Conversion specifier
    By babu in forum C Programming
    Replies: 1
    Last Post: 06-25-2007, 12:09 AM
  4. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  5. format specifier
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 05-13-2006, 05:22 AM