473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

extern local variables

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
3 5145
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gernot Frisch | last post by:
Problem: A.cpp: ------ static FOO* gFoo=NULL; A.h extern FOO* gFoo; gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
4
by: John Ratliff | last post by:
I have a few global variables in my app. They are defined in the main application class file and declared (extern) in a header which can be included by anyone who would want to use these variables....
6
by: atv | last post by:
Alright, i have some questions concerning include files en global variables.I hope someone is willing to answer these. 1).Why is it that if i define a global variable in a file, say main.c, and...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
18
by: StephQ | last post by:
I'm facing the following problem: I'm using the Gnu Scientific Library (it is a C math library), and in particular random number generation algorithms. Example code is: .... gsl_rng * r; .......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.