extern

suggest change

The extern storage class specifier can modify a declaration in one of the three following ways, depending on context:

  1. It can be used to declare a variable without defining it. Typically, this is used in a header file for a variable that will be defined in a separate implementation file.
// global scope
int x;             // definition; x will be default-initialized
extern int y;      // declaration; y is defined elsewhere, most likely another TU
extern int z = 42; // definition; "extern" has no effect here (compiler may warn)
  1. It gives external linkage to a variable at namespace scope even if const or constexpr would have otherwise caused it to have internal linkage.
// global scope
const int w = 42;            // internal linkage in C++; external linkage in C
static const int x = 42;     // internal linkage in both C++ and C
extern const int y = 42;     // external linkage in both C++ and C
namespace {
    extern const int z = 42; // however, this has internal linkage since
                             // it's in an unnamed namespace
}
  1. It redeclares a variable at block scope if it was previously declared with linkage. Otherwise, it declares a new variable with linkage, which is a member of the nearest enclosing namespace.
// global scope
namespace {
    int x = 1;
    struct C {
        int x = 2;
        void f() {
            extern int x;           // redeclares namespace-scope x
            std::cout << x << '\n'; // therefore, this prints 1, not 2
        }
    };
}
void g() {
    extern int y; // y has external linkage; refers to global y defined elsewhere
}

A function can also be declared extern, but this has no effect. It is usually used as a hint to the reader that a function declared here is defined in another translation unit. For example:

void f();        // typically a forward declaration; f defined later in this TU
extern void g(); // typically not a forward declaration; g defined in another TU

In the above code, if f were changed to extern and g to non-extern, it would not affect the correctness or semantics of the program at all, but would likely confuse the reader of the code.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Storage class specifiers:
* extern
* static
* auto

Table Of Contents
8 Arrays
11 Loops
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
124 Storage class specifiers
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype