extern

extern declarator      // used when variable or function has external linkage

extern string-literal declarator      // used when linkage conventions of another
                          // language are being used for the declarator

extern string-literal { declarator-list }   // used when linkage conventions of another
                         // language are being used for the declarators

The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. In C++, when used with a string, extern specifies that the linkage conventions of another language are being used for the declarator(s).

Declarations of variables and functions at file scope are external by default.

In C++, string-literal is the name of a language. The language specifier "C++" is the default. "C" is the only other language specifier currently supported by Microsoft C/C++. This allows you to use functions or variables defined in a C module.

All of the standard include files use the extern "C" syntax to allow the run-time library functions to be used in C++ programs.

For more information, see auto, register, static, const, and volatile.

Example

The following example declares the functions printf, getchar, and putchar with “C” linkage:

// Example of the extern keyword
extern "C" int printf( const char *, ... );

extern "C"
{
   int getchar( void );
   int putchar( int );
}