about extern declaration

This is a discussion on about extern declaration within the C Programming forums, part of the General Programming Boards category; i know that if we declare a variable in one source file then we have to declare that same variable ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you will have linker error due to duplicate external symbol, I think
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 11: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, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21