Extern Variable...

S

Shraddha

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???
 
E

Eric Sosman

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.
 
J

Joe Wright

Shraddha said:
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???
Eric Sosman has responded fully. He is much higher in the pecking order
than I and you will ignore him at some peril. :)

1. No need to declare anything static. All variables at file scope have
static duration. They have external linkage. If I would link two
translation units such that they share a 'global' variable, I would
declare 'extern int global;' in a header file to be included in all
translation units which care about 'global' and in one and only one of
the translation units, define at file scope 'int global = 0;'. All
translation units have equal access to this single variable. Vaya con Dios.
 
E

Eric Sosman

Joe said:
Eric Sosman has responded fully. He is much higher in the pecking order
than I and you will ignore him at some peril. :)

Peck peck peck peckity peckpeckitypeckpeckpeck -- oh, sorry;
thought you were a dead aspen with a tasty grub.

(But he should still listen to both of us anyhow, birdbrains
or no.)
 
B

Barry Schwarz

Eric Sosman has responded fully. He is much higher in the pecking order
than I and you will ignore him at some peril. :)

1. No need to declare anything static. All variables at file scope have
static duration. They have external linkage. If I would link two

It is possible to declare an identifier at file scope with the static
storage class. In this case, the identifier has internal, not
external, linkage.
translation units such that they share a 'global' variable, I would
declare 'extern int global;' in a header file to be included in all
translation units which care about 'global' and in one and only one of
the translation units, define at file scope 'int global = 0;'. All
translation units have equal access to this single variable. Vaya con Dios.


Remove del for email
 
S

Spiros Bousbouras

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.


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.
 

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
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top