[comp.lang.c] static stuff...

swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) (11/18/87)

First, what is the significance of making a static function?

Second, aren't all global variables automatically static.  Is there any
reason to explicitly call them static?

swarbric@tramp.UUCP

ljz@fxgrp.UUCP (11/18/87)

In article <3011@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes:
>First, what is the significance of making a static function?
>
>Second, aren't all global variables automatically static.  Is there any
>reason to explicitly call them static?

Yes, all global variables are static, as are all functions, for that
matter.

The "static" keyword in front of a global variable or a function is
used to indicate that the variable or function is only known within
the file it is defined in.  For example, consider the following two
files, foo.c and bar.c:

/* beginning of file foo.c */

int foo_1 = 0;
static int foo_2 = 0;

foo_func_1()
{
    return foo_1;
}

static foo_func_2()
{
    return foo_2;
}

/* end of file foo.c */

/* beginning of file bar.c */

extern int foo_1;
extern int foo_2;

extern int foo_func_1();
extern int foo_func_2();

main()
{
    printf("foo_1 = %d\n", foo_1);
    printf("foo_2 = %d\n", foo_2);
    printf("foo_func_1() returns %d\n", foo_func_1());
    printf("foo_func_2() returns %d\n", foo_func_2());
}

/* end of file bar.c */

Compiliong the files foo.c and bar.c separately will result in no errors
(unless I made a typo somewhere).  However, if you try to link them,
you will find that the variable "foo_2" and the function "foo_func_2"
are unknown references in the routine "main".  This is because they
are only known within the file foo.c due to the "static" keyword.

Although using the "static" keyword in this way might be a bit confusing,
the feature is a nice one, as it minimizes the number of external symbols
that occur.  If I create a library and use a word like, say, "counter"
as a global within one of the files I'm compiling from, but that
variable is only known within that file, I can declare it to be a
static global.  That way, if you link to my library and happen to
use your own global (static or otherwise) named "counter", there
won't be a name conflict.  Same goes for functions.

---
Lloyd Zusman, Master Byte Software, Los Gatos, California
"We take things well in hand."
...!ames!fxgrp!ljz

carroll@snail.UUCP (11/18/87)

	Yes. Static variables have their names hidden from code in other files.
It can make a big diffence.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (11/18/87)

In article <3011@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes:
>First, what is the significance of making a static function?
>Second, aren't all global variables automatically static.  Is there any
>reason to explicitly call them static?

"static" is a C keyword that is heavily overloaded.  It not only is used
to mean "static storage duration" (as opposed to dynamically allocated),
but it also is used to indicate "file scope" (as opposed to global scope).
Sometimes it means both!

The main reason for attaching "static" to a top-level function or datum
in a source file is to prevent its name being visible to object modules
produced from other source files (other "translation units").  A static
datum is also pre-initialized (with 0s of appropriate type if no explicit
initializer is given).

roth@mrsvr.UUCP (Dean Roth) (11/18/87)

In article <3011@sigi.Colorado.EDU>, swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes:
> First, what is the significance of making a static function?
> 
> Second, aren't all global variables automatically static.  Is there any
> reason to explicitly call them static?
> 
> swarbric@tramp.UUCP

Global and static global (extern) variables are not the same thing.
A static global can only be referenced by code in the same source code
file. A non-static global can be referenced by any function, even if it
is in a different source file and separately compiled and linked.
 

One reason to declare a global variable static is to restrict its scope-
only functions in that same source file can reference the variable. 

peacock%mills-c@tut.cis.ohio-state.edu.UUCP (11/18/87)

	Declaring functions and globals as static causes them not to be
externally defined.  They are local to the source file they are declared in.
If you have two source files with int foo() declarations, the linker will
complain when you link them.  Declaring them static int foo() fixes this.

throopw@xyzzy.UUCP (Wayne A. Throop) (11/18/87)

> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN)
> First, what is the significance of making a static function?
> Second, aren't all global variables automatically static.  Is there any
> reason to explicitly call them static?

Hoooooboy.  I have a great deal of sympathy for poor folks trying to
program with no reference manuals of their own, nor even access to the
reference manuals in libraries and the like.  That is, after all, the
only explanation for such a question I can imagine.  Let me help out
here.  I'll take the most basic and widespread C reference, K&R, look up
"static" in the index, refer to page 80 as the index indicates, and find
that

        An external static variable is known within the remainder of the
        source file in which it is declared, but not in any other file.
        External static thus provides a way to hide names [...]
        It is possible for a function to be declared static; this makes
        its name unknown outside of the file in which it is declared.
        In C, "static" connotes not only permanence but also a degree of
        what might be called "privacy".  Internal static objects are
        known only inside one function; external static objects
        (variables or functions) are known only within the source file
        in which they appear [...]

Harbison and Steele, my compiler reference manual, and practically every
other C reference material I can find to check say similar things in the
entries indexed by "static".  As I say, I can only imagine the hardship
some people are subjected to in attempting to use a language with no
reference material.  Why the university or firm such a person belongs to
permits such poor conditions is beyond me.  These people have my utmost
sympathy.

--
"When The Cow MOOS... udders TREMBLE!"
        --- The Cow, speaking to Madame Marsupial
            (from The New Adventures of Mighty Mouse)
-- 
Wayne Throop      <the-known-world>!mcnc!rti!xyzzy!throopw

sdejarne@polyslo.UUCP (Steve DeJarnett) (11/19/87)

In article <3011@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU writes:
>First, what is the significance of making a static function?
	Static functions are only callable in the source file that they are
defined in.  This way, you can make protected functions (although, if you
only have one source file, their use makes less sense).
>Second, aren't all global variables automatically static.  Is there any
>reason to explicitly call them static?
	All automatic variables are NOT static.  Static variables declared
inside a function retain their value between function calls (i.e. if a function
with a static variable is called more than once, the value of the static 
variable from the first call is preserved.), whereas automatics lose their
value between function calls (they are re-initialized at each call).  If you
only call a function once, then it may appear that the automatics you defined
behave just like statics, but there is a difference.  If you define static 
variables outside of a function body, they are only visible to functions BELOW
themselves in the source file.  They are not visible to functions in different
source files.  

	I'm fairly sure all of this is correct, but if not, I'm sure some C
guru will let me know :-).    

	Hope this helps.


-------------------------------------------------------------------------------
| Steve DeJarnett		|    ...!ihnp4!csun!polyslo!sdejarne	      |
| Computer Systems Lab		|    ...!{csustan,csun,sdsu}!polyslo!sdejarne |
| Cal Poly State Univ		|    ...!ucbvax!voder!polyslo!sdejarne	      |
| San Luis Obispo, CA  93407	|    					      |
-------------------------------------------------------------------------------
#include <std_disclaimer.(ks

swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) (11/19/87)

I do have referance material (Turbo C Reference Guide, C Primer Plus, Advanced
Turbo C, and a few others), I just did not bother to look it up because I
didn't think there was even a differnce.  After I found out there was a
difference I looked back and the books do mention it, but I had totally
forgotten about it.
.

swarbrick@tramp.Colorado.EDU

daveb@laidbak.UUCP (Dave Burton) (11/19/87)

In article <3011@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes:
>First, what is the significance of making a static function?
>
>Second, aren't all global variables automatically static.  Is there any
>reason to explicitly call them static?

Static has two meanings in C:
1.	opposite of dynamic
2.	do not export symbol

To answer both your questions:
Declare global objects (that includes function names) static to
prevent them from being seen outside of the file they live in.

Proper use of static in large, multiple file programs helps prevent
symbol collision.
-- 
--------------------"Well, it looked good when I wrote it"---------------------
 Verbal: Dave Burton                        Net: ...!ihnp4!laidbak!daveb
 V-MAIL: (312) 505-9100 x325            USSnail: 1901 N. Naper Blvd.
#include <disclaimer.h>                          Naperville, IL  60540