Static variables.

U

ur8x

Hi,

I was wondering if there is a difference between declaring a
static variable outside of the method as oppose to declaring
it as a local variable?

Thanks.
 
E

Eric Sosman

Hi,

I was wondering if there is a difference between declaring a
static variable outside of the method as oppose to declaring
it as a local variable?

Visibility (more formally known as "scope").

static int outside = 42;
void func1(void) {
static int inside = 17;
printf ("outside = %d\n", outside);
printf ("inside = %d\n", inside);
}
void func2(void) {
printf ("outside = %d\n", outside); /* OK */
printf ("inside = %d\n", inside); /* error */
}

The variable `outside' is visible to all functions that
appear after it in the source file, but the variable `inside'
is visible only within func1().
 
S

Strix Nihildom

I don't think there is. Static variables are only initialized once no matter
where they are called for the life of the prog.
 
A

Alex Monjushko

I was wondering if there is a difference between declaring a
static variable outside of the method as oppose to declaring
it as a local variable?

There are no 'methods' in C. I think you mean 'function'.

The only difference is scope. A file scope static variable has
file visibility, while a function scope static variable is only
visible within the function in which it is declared.
 
S

sathya_me

First time replayed to a thread.

Hi,

I was wondering if there is a difference between declaring a
static variable outside of the method as oppose to declaring
^^^^^^

What does the word oppose in this context is? Please explain
it as a local variable?

Thanks.

Google's search in the comp.lang.c with the key word "static" is
throwing
lots of information.Have a look.

--
"Combination is the heart of chess"
A.Alekhine
Mail to:
sathyashrayan25 AT yahoo DOT com
(remove the AT and DOT)
 
S

Sydney Faria

Isn't a local static variable assigned to a memory area that is neither on
the stack and not part of the normal heap? That's how the local static
variable can be used to remember state within a function between calls to
the function. Also, since C never had anything like namespaces, global
static variables where one way to control name collisions: that is one of
the reasons for developing C++ and other OO languages in the next
generation. SCF
 
C

CBFalconer

Sydney said:
Isn't a local static variable assigned to a memory area that is
neither on the stack and not part of the normal heap? That's
how the local static variable can be used to remember state
within a function between calls to the function. Also, since C
never had anything like namespaces, global static variables
where one way to control name collisions: that is one of the
reasons for developing C++ and other OO languages in the next
generation. SCF

Please don't toppost. I fixed this one.

C doesn't have heaps, stacks, etc. Those are things specific to
the implementation, which must do the appropriate incantations to
magically satisfy the C standard on the available hardware. The
point is that static variables retain their values even when the
scope in which they are declared is left. They are also not
visible outside the compilation unit.

C does have namespaces. Certain names are reserved for the
implementation, especially those beginning with a '_'. In
addition the namespaces for struct and enum tags are separate from
user namespace. What C doesn't do is enforce those restrictions.

C++ is NOT a next generation, it is a separate (and different)
language, with some superficial similarities to C and much higher
complexity. Opinions as to whether it is an improvement vary.
 
I

Imanpreet Singh Arora

CBFalconer said:
Please don't toppost. I fixed this one.

C doesn't have heaps, stacks, etc. Those are things specific to
the implementation, which must do the appropriate incantations to
magically satisfy the C standard on the available hardware.


Hmm... I wanted to ask this question for some time now. What do you say if C
does indeed define the binary standard? Or maybe some sort of intermediate
binary standard? Won't it make life for C programmers better? For that
matter I have come across some independent implementations that do provide
binary independence. But the trouble with them is that they are just not at
all standard.



<snip>
 
K

Keith Thompson

Imanpreet Singh Arora said:
Hmm... I wanted to ask this question for some time now. What do you say if C
does indeed define the binary standard? Or maybe some sort of intermediate
binary standard? Won't it make life for C programmers better? For that
matter I have come across some independent implementations that do provide
binary independence. But the trouble with them is that they are just not at
all standard.

I'm not quite sure what you mean by "binary standard".

C is implemented on a wide variety of platforms, some of them quite
exotic compared to the ones you might work on every day. The more C
specifies about binary interfaces, the more difficult it is to
implement it on exotic systems.
 

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
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top