create new tag
, view all tags
The C keyword "extern" can be a little confusing, especially because there is a variant (extern "C") that has a somewhat different meaning.

I'll start by listing some resources I've found that look useful, later I'll try to synthesize my own explanation.




from http://www.clipx.net/ng/turboc/ng7018f.php:

extern                   External Data Storage Class

    When used with a variable, the extern class specifies that the object
    is defined elsewhere in the program as an external, possibly in the
    same source code file.  The extern keyword may be used both
    internally and externally to a function.  An external variable has
    permanent life and can be accessed from any file in which it was
    declared (using extern).  If the extern declaration is specified
    inside a function, the object's scope is limited to the block in
    which it is declared, and all subordinate blocks (if any).  The scope
    of an extern object declared outside a function is from the point of
    declaration through the end of the source code file.  The form of an
    extern declaration for a variable is:

            extern [type] identifier [[, identifier] ... ] ;

    When used with a function definition, the extern class causes that
    function's name to be made global; that is, it can be called from any
    function in the program.  This is the opposite of `static'.  The form
    when used with a function definition is:

            [extern] [type] function-name ([argument-list])

    The extern class can also be used with a function declaration,
    indicating that function is defined elsewhere, possibly in the same
    source code file.  The form when used with a function declaration is:

            [extern] [type] function-name ([argument-list]) ;

      Notes:    Variables with the extern class may not have explicit
                initializers, since they refer to variables that are
                defined elsewhere.

                Since by default all functions have class extern, the
                keyword is rarely used either in function definitions or

                If a function is called before it has been declared, it
                is assumed to have class extern and type int.

                If the type of an extern variable is omitted, int is

  -------------------------------- Example ---------------------------------

           extern int max, total;
           extern struct date today;
           extern int test();

           extern int testchar()
                extern int i;
                extern double average();


See Resource Recommendations. Feel free to add additional resources to these lists, but please follow the guidelines on ResourceRecommendations including Guidelines_for_Rating_Resources.


I started with a Google search on [C extern]:

  • (rhk) Variable Storage Classes; ; — definitely worth reading (for me); covers extern, static, auto, and register — didn't we all just know the word "class" sould be overloaded (multiple (conflicting) definitions within C/C++)
  • (rhk) [[][]]; ; —
  • (rhk) [[][]]; ; —
  • (rhk) Turbo C: extern: External Data Storage Class; ; — This and the next several links seem clear but are based on Turbo C — not sure they are (still) 100% correct / applicable
  • (rhk) [[http://www.clipx.net/ng/turboc/ng74176.php][Turbo C: static: Static Data Storage Class
]]; ; —

Recommended for Specific Needs

  • (rhk) YoLinux Tutorial: Using C and Fortran together:; ; — "This tutorial covers mixing C and FORTRAN together, allowing C to call FORTRAN functions and FORTRAN to call C functions." — this provides some different perspective, and this line seemed illuminating (in general): "The extern statement states that it is trying to reference memory which has already been set aside elsewhere."
  • (rhk) A Sharp Angle On Function Pointers;
Jonathan Schilling; ; — Not read yet, but looks useful. (TWiki is being a royal pain today — slow and erratic saving, which makes me waste time saving instead of reading.) Wow, skimmed this, a fairly subtle technicality, and for me, hints at / reinforces the notion that C (and hence C++) are (or at least were, since the orginal design) just thrown together instead of carefully designed. At first, I think of that as a criticism as compared to Pascal, LISP, APL, Algol, or some other languages, but then I remember that, for example, Niklaus Wirth (the designer of Pascal) limited the scope of Pascal, presumably at least partially to make it easier to design (and teach from) — C is a language that had to go out and get its hands dirty, get the job done, so maybe that's the reason some of these "features" in C are so strange. Some other examples (maybe): the two different definitions / uses for static (on http://www.ssec.wisc.edu/~dglo/c_class/vstorage.html).
C++ has a special keyword to declare a function with C bindings: extern "C". A function declared as extern "C" uses the function name as symbol name, just as a C function. For that reason, only non-member functions can be declared as extern "C", and they cannot be overloaded.

Although there are severe limitations, extern "C" functions are very useful because they can be dynamically loaded using dlopen just like a C function.

This does not mean that functions qualified as extern "C" cannot contain C++ code. Such a function is a full-featured C++ function which can use C++ features and take any type of argument.

Another clue from the same source: "In C++ functions are loaded just like in C, with dlsym. The functions you want to load must be qualified as extern "C" to avoid the symbol name being mangled."

and: "There are two different forms of the extern "C" declaration: extern "C" as used above, and extern "C" { … } with the declarations between the braces. The first (inline) form is a declaration with extern linkage and with C language linkage; the second only affects language linkage. The following two declarations are thus equivalent:"


  • () RandyKramer - 02 May 2003
  • If you edit this page: add your name here; move this to the next line; and if you've used a comment marker (your initials in parenthesis), include it before your WikiName.

Page Ratings

Topic revision: r1 - 2003-05-02 - RandyKramer
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by PerlCopyright 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding WikiLearn? WebBottomBar">Send feedback
See TWiki's New Look