Thread: about extern declaration

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    about extern declaration

    i know that if we declare a variable in one source file then we have to declare that same variable with extern prefix in some other source file which are to be linked later while compiling. soppose we have 3 source files and among those 3 files 2 have declared a variable without extern prefix and one is declared with extern prefix. so which decleration is considered by the source file which has declared that variable with extern prefix.

    ex:

    file1.c-----

    //global variables
    int a = 6;

    file2.c------
    //global variables
    int a = 3;


    file3.c
    //global variables
    extern int a;


    so in file3.c which definition is considered????

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you will have linker error due to duplicate external symbol, I think
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    what if we dont initialise one of that.. i mean in file1.c i will just take ...int a.i tried it in unix it works successfully... what does it mean actually??? how it links in this example.....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you only initialize one "int a", then any declaration "int a;" (without initialization) will be equivalent to "extern int a;".

    Multiple initializations should generate a linker error for multiple symbols of the same name (or whatever the linker calls it).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    as we compile each source files seperately and link them after that. while compiling each file seperately compiler will allocate seperate memory if we declare the variable as just int a (not with extern prefix).. so how will compiler handles this when linking the files..

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jam_fiveface View Post
    as we compile each source files seperately and link them after that. while compiling each file seperately compiler will allocate seperate memory if we declare the variable as just int a (not with extern prefix).. so how will compiler handles this when linking the files..
    Because it has a special directive to the linker to say "This variable is called a, and it takes up 4 bytes - make one space for it". You'd get very interesting effect, by the way if you do something like this:
    Code:
    // main.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int a;
    
    int main()
    {
        printf("%d (%08x)\n", a, a);
        return 0;
    }
    
    // b.c
    float a = 1.0;
    It will print some huge number in the 1 billion range (and 3f800000 in hex - I know that one).

    Note that g++ (instead of gcc) causes the above code to generate an error for "multiple defined symbols".
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM