473,379 Members | 1,222 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,379 software developers and data experts.

Extern Variable...

Can we declare extern variable as static?
What will be the scope of the variable then? What if we change the
value of the variable in some other function?

Also can someone tell me that if we can declare the global
variable...and it is having scope throughout the file then what is so
different in extern variable???

Jun 25 '07 #1
2 4677
Shraddha wrote On 06/25/07 09:25,:
Can we declare extern variable as static?
What will be the scope of the variable then? What if we change the
value of the variable in some other function?

Also can someone tell me that if we can declare the global
variable...and it is having scope throughout the file then what is so
different in extern variable???
You seem to be confused about the two different
notions of "scope" and "linkage." (This confusion is
understandable, in part because people throw the word
"scope" around in sloppy fashion.)

In C, the scope of an identifier is the part of a
translation unit ("module" or "compilation") where a
particular declaration of the identifier is in force.
Different kinds of declarations have different scopes
that obey different rules: For example, the scope of
a variable or function declared inside a { } block ends
at the closing }, while the scope of a declaration that
is outside any block extends to the end of the module.
Scopes may be "interrupted" by competing declarations:

int foo;
void bar(void) {
long x = foo; /* referring to the int */
double foo = 9; /* a competing declaration */
printf ("%g\n", foo); /* refers to the double */
}
void baz(void) {
printf ("%d\n", foo); /* refers to the int again */
}

There are two different declarations of `foo' here. The
scope of the first one starts at its declaration and
runs to the end of the whole module, but is interrupted
by the second declaration inside bar(). The scope of
the second `foo' starts at *its* declaration and runs
to the closing } at the end of the block, at which point
the scope of the original `foo' resumes.

Linkage is an entirely different matter. It governs
whether identical identifiers in different scopes refer
to the same object/function or to unique ones. When an
identifier has "external linkage," all its scopes in all
the program's translation units are gathered together and
made to refer to the same thing. When an identifier has
"internal linkage" or "no linkage," the scopes are kept
apart: Each scope's use of the identifier refers to its
own unique object or function.

"Global variable" is a loose and undefined term; you
can use it informally but should avoid it when you need
to discuss something precisely.

--
Er*********@sun.com
Jun 25 '07 #2
On 25 Jun, 14:25, Shraddha <shraddhajosh...@gmail.comwrote:
Can we declare extern variable as static?
What will be the scope of the variable then? What if we change the
value of the variable in some other function?
It is not clear what you mean by "extern variable". If you mean
a variable with file scope then the answer is yes , you can declare
it as static. But if you mean something like
extern static int a ;
then it is a constraint violation and requires a diagnostic. In 6.7.1
of n1124 we read:

At most, one storage-class specifier may be given in the
declaration specifiers in a declaration.
On 26 Jun, 01:42, Joe Wright <joewwri...@comcast.netwrote:
1. No need to declare anything static. All variables at file scope have
static duration. They have external linkage.
If you don't want it to have external linkage then you should declare
it
as static. Even if you don't care either way, if it appears in only
one
translation unit, declaring it static will avoid (sp)lint warnings.

Jun 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: mark | last post by:
compiler g++ on linux file.h has the following, int x=5; and inside the main program file I can access "x" without declaring it "extern x". Program compiles and runs fine.... also program...
2
by: Sona | last post by:
Hi, I read some place that if you have two or more modules or files in C, you can share variables between the these modules or files using the 'extern' keyword. I'm not really sure how to do...
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 ;
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
4
by: gaurav.dube | last post by:
I am facing a problem with warning removal I have got a header file it contains extern declarations of lot of variables (say 100) This header file is included in hundreds of .c files. Some of...
9
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I...
2
by: Shraddha | last post by:
When I read about volatile keyword....while explaning there was an declaration like... extern const a; But as a rule const shoule be initialised where it is defined...but we do ot initialise...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.