By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,376 Members | 1,599 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,376 IT Pros & Developers. It's quick & easy.

extern local variables

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In <2d**************************@posting.google.com > j0******@engineer.com (j0mbolar) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #2

P: n/a
On 4 Mar 2004 05:13:31 -0800, j0******@engineer.com (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.


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
le**@bdsoft.com
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
Nov 14 '05 #3

P: n/a
j0******@engineer.com (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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
OOOOOOOOOO! NAVY SEALS!
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.