Can someone explain "extern" to me?

This is a discussion on Can someone explain "extern" to me? within the C++ Programming forums, part of the General Programming Boards category; Can anyone explain to me how to use extern? I've seen it used all over, but I can't understand how ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Unhappy Can someone explain "extern" to me?

    Can anyone explain to me how to use extern? I've seen it used all over, but I can't understand how to link a file and how to use it. If you can explain that, can you also explain how to use extern and #define in header files?

    Thanks in advance,
    Valar King
    -Save the whales. Collect the whole set.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    17
    You said:

    "Can anyone explain to me how to use extern? I've seen it used all over, but I can't understand how to link a file and how to use it. If you can explain that, can you also explain how to use extern and #define in header files?"

    ANSWER #1:
    extern is used to reassure the compiler that just because a variable is not resolved in the source file or #included headers, that it will be resolved at link time (after compile time). If I declare:

    int x;

    in main.c and I reference it in secondary.c, I must put the line:

    extern int x;

    in secondary.c so the compiler knows that this variable is originally declared in a separate source file. You cannot simply declare "int x" again in secondary.c because this would create a separate variable, not a shared one.

    ANSWER #2:
    #define is a preprocessor instruction to literally replace a defined string with it's value. When you include this line:

    #define MAXLENGTH 100

    The preprocessor does nothing but literally replace all instances of MAXLENGTH with the string 100. Therefore, something that is #defined does not have any variable type and is constant in nature (not constant as in using the const keyword, that is a separate concept)

    Thanks to Nick for clarification on the subject.
    Last edited by sh0x; 09-11-2001 at 07:35 AM.
    -sh0x

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    extern tells the linker that the symbol is definined externally
    or it's defined in a different translation unit. About #defines, it's the preprocessor not the compiler. Before compiling all the preprocessor directives such as #define, #include are done. After the preprocessor is done you are left with the translation unit.

  4. #4
    edwardtisdale
    Guest

    Post Extern

    If you have two .cpp files and you want something in one .cpp file to be used in another .cpp file use extern. It is like extending global. Global can be used throughout one .cpp file and extern can also, but can also be used in another. It reminds me of a derived class having a virtual function that overrides the same function that is defined in the base class (like a child window). I guess if files were windows a file with an extern function would be a child of a file with a global function where the base defines globally for example int CreateaSchedule(); and the derived would have extern int CreateaSchedule();

    www.edwardtisdale.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  2. Can someone explain "extern" to me (again)?
    By jas_atwal in forum C++ Programming
    Replies: 8
    Last Post: 01-03-2008, 07:07 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 11:36 AM
  4. explain this loop statement?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-05-2002, 02:46 AM
  5. Pls explain how this program works...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 01-05-2002, 08:53 AM

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