Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Extern Variable... (http://www.velocityreviews.com/forums/t517468-extern-variable.html)

Shraddha 06-25-2007 01:25 PM

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


Eric Sosman 06-25-2007 02:38 PM

Re: Extern Variable...
 
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.

--
Eric.Sosman@sun.com

Joe Wright 06-26-2007 12:42 AM

Re: Extern Variable...
 
Shraddha wrote:
> 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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Eric Sosman 06-26-2007 12:55 AM

[OT] Re: Extern Variable...
 
Joe Wright wrote:
> Shraddha wrote:
>> 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. :-)


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

--
Eric Sosman
esosman@acm-dot-org.invalid

Barry Schwarz 06-26-2007 01:10 AM

Re: Extern Variable...
 
On Mon, 25 Jun 2007 20:42:32 -0400, Joe Wright
<joewwright@comcast.net> wrote:

>Shraddha wrote:
>> 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


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

Spiros Bousbouras 06-26-2007 01:19 AM

Re: Extern Variable...
 
On 25 Jun, 14:25, Shraddha <shraddhajosh...@gmail.com> wrote:
> 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.net> wrote:

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



All times are GMT. The time now is 08:36 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.