* Stupid Question.
@ 2004-05-12 18:53 John T. Williams
2004-05-12 19:02 ` Jeff Woods
0 siblings, 1 reply; 4+ messages in thread
From: John T. Williams @ 2004-05-12 18:53 UTC (permalink / raw)
To: Linux-C-Programming (E-mail)
I know I should know this, but I don't, and haven't been able to find a
good answer.
What does declaring a function static do? ie
What is the difference between
static int blue() {
return 0;
}
and
int blue() {
return 0;
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid Question.
2004-05-12 18:53 Stupid Question John T. Williams
@ 2004-05-12 19:02 ` Jeff Woods
2004-05-13 14:38 ` Jan-Benedict Glaw
0 siblings, 1 reply; 4+ messages in thread
From: Jeff Woods @ 2004-05-12 19:02 UTC (permalink / raw)
To: John T. Williams; +Cc: Linux-C-Programming (E-mail)
At 5/12/2004 02:53 PM -0400, John T. Williams wrote:
>What does declaring a function static do? ie
>What is the difference between
>
>static int blue() {
> return 0;
>}
>
>and
>
>int blue() {
> return 0;
>}
It makes the function visible only within the comilation unit it's in. See
also:
http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/static.htm
As the website points out static functions and static variables seem like
very different meanings of the word "static". In both cases I think of it
as "global but private/hidden".
--
Jeff Woods <kazrak+kernel@cesmail.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid Question.
2004-05-12 19:02 ` Jeff Woods
@ 2004-05-13 14:38 ` Jan-Benedict Glaw
2004-05-13 18:27 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Jan-Benedict Glaw @ 2004-05-13 14:38 UTC (permalink / raw)
To: Linux-C-Programming (E-mail)
[-- Attachment #1: Type: text/plain, Size: 1076 bytes --]
On Wed, 2004-05-12 13:02:25 -0600, Jeff Woods <Kazrak+kernel@cesmail.net>
wrote in message <6.0.1.1.0.20040512125919.02e8cec0@no.incoming.mail>:
> At 5/12/2004 02:53 PM -0400, John T. Williams wrote:
> >What does declaring a function static do? ie
> >What is the difference between
> >static int blue() {
> > return 0;
> >}
> >and
> >int blue() {
> > return 0;
> >}
>
> It makes the function visible only within the comilation unit it's in. See
> also:
A static function (or variable) will only be "visible" within exactly
that .c file which it is defined in.
Static automatic variables (those that are within functions) do have a
different semantic: their contents is preserved during multiple function
calls.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak!
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Stupid Question.
2004-05-13 14:38 ` Jan-Benedict Glaw
@ 2004-05-13 18:27 ` Glynn Clements
0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2004-05-13 18:27 UTC (permalink / raw)
To: Linux-C-Programming (E-mail)
Jan-Benedict Glaw wrote:
> Static automatic variables (those that are within functions) do have a
> different semantic: their contents is preserved during multiple function
> calls.
"Static automatic variables" is a self-contradiction. "Static local
variables" (as opposed to "automatic local variables") would be more
accurate.
Local variables (those whose scope is limited to the block in which
they are declared) are automatic (stored on the stack) by default. The
"static" keyword makes them static (stored in the data segment).
This overloading of the "static" keyword is a common source of
confusion. They really should have added another keyword, IMHO. [And
dispensed with the (entirely redundant) "automatic" keyword, which
I've never actually seen used in nearly 20 years of C programming.]
And the distinction between automatic and static local variables is
somewhat deeper than the fact that the value is preserved across
calls. It's highly significant when dealing with reentrancy (e.g.
recursive functions, multi-threaded code).
For instance, an automatic variable has a separate instance for each
call, while a static variable only has a single instance. OTOH, you
can legitimately return or store a pointer to a static variable and
reference it from outside of the function, whereas an automatic
variable ceases to exist once the function returns.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-05-13 18:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-05-12 18:53 Stupid Question John T. Williams
2004-05-12 19:02 ` Jeff Woods
2004-05-13 14:38 ` Jan-Benedict Glaw
2004-05-13 18:27 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).