Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Extern keyword in C language

  1. Aug 7, 2011 #1
    Can you explain the working mechanism of extern keyword in C language? I have tried wiki and other sites but couldn't follow. Please explain in simple english.
     
  2. jcsd
  3. Aug 7, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi pairofstrings! :smile:

    I'll give it a try...

    The extern keyword in front of an identifier tells the compiler that the actual definition of the identifier is supposed to be in another .c file.
    After that you can use the identifier in your current .c file as if you had completely defined it.

    Putting extern in front of a function declaration (without body) tells the compiler how the function should be called, so you can call the function in the current .c file.
    The actual function definition with its body can then be defined in another .c file.

    Extern can also be put in front of a variable declaration.
    Perhaps this is a bit more confusing.
    Doing that tells the compiler you want to share the variable between different .c files.
    However, there has to be 1 .c file where the variable is declared without the extern in front of it.
     
  4. Aug 7, 2011 #3
    I like Serena explained it, but I'd like to explain the background.

    When source code is compiled, it is turned into "object code." Object code is a medium between the final executable and the original source code.

    In object code, everything that can be accessed from another object code is given a name, referred to as a "symbol." For example, functions and global variables have symbols. The object code can also depend on symbols it doesn't contain. For example, it can call a function that isn't defined in the source code it was compiled from.

    After all the objects have been compiled, they are sent to a linker that links the objects into a single executable. The linker makes sure that all the symbol dependencies are met. (In other words, it makes sure that, if one of the objects calls a function called "foo," then "foo" is defined in one of the objects or a library. Libraries are collections of symbols and their definitions.)

    The extern keyword is to tell the compiler that the variable is defined somewhere else, and that the compiler should make that variable a dependency.
     
  5. Aug 7, 2011 #4

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    The relevant buzzwords in the C standard (and probably in your documentation) are "declaration" and "definition".

    In a complete C program, each object (data item, function, etc) can only be DEFINED once. But if the source code is split into several different files, you have to compiler what the name means (in other words, DECLARE it) in every file that USES the name.

    So, if your program uses a global array of integers called foo, there will be just ONE source file DEFINES foo, by sayng something like
    Code (Text):

    int foo[100];
     
    In the other files that use foo, you need to tell the compiler "foo is an integer array, but it's actually defined somewhere else". You do that by saying
    Code (Text):

    extern int foo[];
     
    Note, you don't need to tell the compiler how big the array is when you are only declaring it, because the compiler isn't going to allocate any storage for the array - you are just telling the compiler that foo IS an array.

    Usually, each declaration appears just once in a header (.h) file, which is included everywhere it is needed. That way, if you suddenly realize that foo needs to be a long int, you only have to change two files (the .h file containing the declaration, and the file containing the definition), not a large number of .c files with the risk of forgetting one of them and introducing a bug into the program.

    To make using header files easier, the compiler doesn't complain if it sees both a declaration and a definition of the same object, provided they don't contradict each other.

    All the above also applies to functions, except that all function declarations are extern by default (because that is what you want them to be, 99.9% of the time!) So the function declarations
    Code (Text):

    int foobar{double};
     
    and
    Code (Text):

    extern int foobar{double};
     
    mean exactly the same thing. So in real life nobody uses "extern" on function declarations, only on variable declarations.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Extern keyword in C language
  1. Assembly language or C (Replies: 5)

Loading...