8

According to wikipedia:

http://en.wikipedia.org/wiki/External_variable

An external variable may also be declared inside a function.

What is the purpose of an extern variable being declared within a function? Would it have to be static too?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • these posts are related: http://stackoverflow.com/questions/16459422/why-does-declaring-an-extern-variable-inside-main-works-but-not-defining-it-in and http://stackoverflow.com/questions/9686198/what-is-the-use-of-declaring-a-static-variable-as-extern-inside-a-function – taocp Jun 01 '13 at 00:11

5 Answers5

4

It allows for restricting access to a global to some scope:

int main()
{
    extern int x;
    x = 42;  //OKAY
}

void foo()
{
    x = 42;   //ERROR
}

int x;
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
3

The external declaration goes inside a function. It simply means that no other functions can see the variable.

void func()
{
   extern int foo;
   foo ++; 
}


void func2()
{
   foo--;     // ERROR: undeclared variable. 
}

In another source file:

int foo;     // Global variable. Used in the other source file, 
             // but only in `func`. 

It is just a way to "isolate" a variable, so it doesn't accidentally get used in places where it isn't supposed to be used.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I don't get how this is different than a regular local variable... How does `extern` make it different? – David G Jun 01 '13 at 00:16
  • @0x499602D2 `extern` tells the compiler 'this variable is defined somewhere else.' It doesn't actually define anything, it just declares. – Collin Dauphinee Jun 01 '13 at 00:18
  • @0x499602D2: It does not define a variable; it _declares_ one that exists elsewhere. It is therefore not "local" at all. – Lightness Races in Orbit Jun 01 '13 at 00:18
  • It is a gloobal variable defined in another source file. Doing `foo++;` on an uninitialized local variable would be undefined behaviour and pointless. Both of which make it a poor idea. – Mats Petersson Jun 01 '13 at 00:20
  • @0x499602D2: I have added some further code-snippets to explain better (I think...) – Mats Petersson Jun 01 '13 at 00:22
  • So I would initialise my variable to say, the value 10 elsewhere but it would only be able to be used within func()? – user997112 Jun 01 '13 at 00:22
  • @user997112: Yes, and of course anywhere else you put an extern declaration, as well as any place that actually has `foo` in the source file [places after it's definition, as compilers can't "see" forwards when finding out what a variable is]. – Mats Petersson Jun 01 '13 at 00:25
2

The only difference between declaring an external variable at namespace scope:

extern int x;

void foo() {
   cout << x;
}

and declaring it at function scope:

void foo() {
   extern int x;
   cout << x;
}

is that in the latter case, x is only visible inside the function.

All you're doing is further tightening the scope of the extern declaration.


Here's an analogous example using namespaces:

At namespace scope:

#include <string>
using std::string;

void foo() {
   string str1;
}

string str2; // OK

At function scope:

#include <string>
void foo() {
    using std::string;
    string str1;
}

string str2; // Error - `using` not performed at this scope
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

The text refers to a non-defining declaration of an extern variable inside function. Extern definitions inside functions are illegal.

So, a non-defining declaration of an extern variable inside function simply means that you want to use that variable inside that function. The variable itself must be a global variable defined elsewhere.

Basically, if you don't need access to that global variable (defined elsewhere) in the entire translation unit and just need it inside that function, it is perfectly good idea to declare it locally. That way you are not polluting global namespace with identifiers no other function needs.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • If it cannot be used anywhere else- could it be argued it might as well be a local static variable? Is there a difference? – user997112 Jun 01 '13 at 00:25
  • @user997112: I'm not sure what you mean by "it cannot be used anywhere else". Actually, the variable itself *can* be used absolutely anywhere. (It is a global variable, after all.) It is just that it will require a separate declaration in other places as well. – AnT stands with Russia Jun 01 '13 at 00:28
  • In the answer above it looks like the variable cannot be used outside of the function it is externally declared in? – user997112 Jun 01 '13 at 15:38
  • @user997112: There a confusion between the notions of "variable" and "identifier that refer to a variable". The *variable* itself is *defined* elsewhere. It is global. Everyone can *declare* that variable and thus gain access to it. Everyone. So, it is just a question of how you declare it. By declaring it locally, you gain access to that variable locally. Any other locality can also declare it locally, and also gain access *to the same variable* locally. – AnT stands with Russia Jun 01 '13 at 17:14
  • @user997112: It is like Internet. There's only one global Internet. Everyone can access it by signing up with ISP. But when one person signs up with ISP, it does not mean that that person allows everyone to use his connection. Only the person who signed up can use the connection. Meanwhile, other people can also sign up with their own ISPs and gain access to the very same Internet. – AnT stands with Russia Jun 01 '13 at 17:17
  • And that's how a global variable works: there's only one global variable, say, `int a;` defined in some file somewhere. Anyone can "establish connection" to that global variable by declaring it as `extern int a;`. Now, this connection can be established locally, inside a function, so that only that function has access to the connection. But that does not prevent any other function to establish its own local connection by making its own `extern int a;` declaration. All such functions will access the same global variable `a`. – AnT stands with Russia Jun 01 '13 at 17:19
0

The extern keyword says that an identifier has external linkage. This means that it is linked to the same name anywhere else it is declared with external linkage. That is, the different instances of the name in different places refer to the same object.

Declaring an identifier inside a block (including the block that defines a function) gives it block scope. The scope of that instance of the identifier ends at the end of the block.

Combining extern with block scope allows a function to see an external identifier without cluttering the name space of other functions. (Nonetheless, it is often bad practice.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312