extern local variables

J

j0mbolar

void myfunction(void)
{
extern int myvariable;
return ;
}

what is the point in allowing local variables to have external linkage?
its scope is only myfunction so it can't be used anywhere else.
 
D

Dan Pop

In said:
void myfunction(void)
{
extern int myvariable;
return ;
}

what is the point in allowing local variables to have external linkage?

You're confused.

extern int myvariable;

does NOT *define* myvariable, it merely *declares* myvariable as an
object defined somewhere else with external linkage.
its scope is only myfunction so it can't be used anywhere else.

The scope of the myvariable *identifier* is only myfunction. The actual
object was *defined* elsewhere and it's still available to any function
*declaring* it.

Example:

fangorn:~/tmp 264> cat test.c
#include <stdio.h>

void foo(void)
{
extern int myvariable;
printf("%d\n", myvariable);
}

void foo2(void)
{
extern int myvariable;
printf("%d\n", myvariable * 2);
}

int main()
{
foo();
foo2();
return 0;
}

int myvariable = 42;

fangorn:~/tmp 265> gcc -ansi -pedantic test.c
fangorn:~/tmp 266> ./a.out
42
84

You can also move the definition of myvariable to another source file.

Dan
 
L

Leor Zolman

void myfunction(void)
{
extern int myvariable;
return ;
}

what is the point in allowing local variables to have external linkage?
its scope is only myfunction so it can't be used anywhere else.

The /name/ may have local scope, but the variable it refers to most
certainly will not. This would be something you might choose to do
(personally, I don't think I've ever written a declaration like this) to
bring a file-scope variable defined in, say, other another translation unit
into scope within the body of this function, in case it isn't in scope
already. (Another possibility is that it is brought into file scope /below/
the point where this function is defined.)

Of course, if myvariable is already declared at file scope at the point
where your function definition resides, this would effectively constitute a
no-op declaration.
-leor



Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
D

Derk Gwen

(e-mail address removed) (j0mbolar) wrote:
# void myfunction(void)
# {
# extern int myvariable;
# return ;
# }
#
# what is the point in allowing local variables to have external linkage?
# its scope is only myfunction so it can't be used anywhere else.

What's the point in disallowing it? It's statically allocated and visible
to all other declarations of the same extern name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,815
Members
47,361
Latest member
RogerDuabe

Latest Threads

Top