[comp.lang.c] Initializing Static Variables

wdelv@devnet3.hac.com (walt del vecchio) (02/26/91)

I've tried searching for the answer in "The C Programming Language" by K & R
as well as in the regularly posted FAQ's without success.
Does the declaration of a static int, for example, establish a variable
with an initial value of zero ?
If so, is it portable ?
Where did I miss the answer in the literature ?
I am using classic C on a Sun computer using SunOS 3.5.

Thanks in advance for taking the time to email me the answer.

 ------------------------------------------------------------------------------
  Walter L. Del Vecchio - Hughes Aircraft Co. - Fullerton, CA - (714) 732-5588
 ------------------------------------------------------------------------------

gwyn@smoke.brl.mil (Doug Gwyn) (02/26/91)

In article <13704@hacgate.UUCP> wdelv@devnet3.UUCP (walt del vecchio) writes:
>Does the declaration of a static int, for example, establish a variable
>with an initial value of zero ?
>If so, is it portable ?

Variables having static storage duration for which no explicit initializers
are specified receive default initial values of zero (of the appropriate
type).  This has been true of most C implementations on UNIX systems, and
is guaranteed for standard conforming implementations.  A lot of existing
application rely on this feature.

scs@adam.mit.edu (Steve Summit) (02/26/91)

In article <13704@hacgate.UUCP> wdelv@devnet3.UUCP (walt del vecchio) writes:
>I've tried searching for the answer in "The C Programming Language" by K & R
>as well as in the regularly posted FAQ's without success.
>Does the declaration of a static int, for example, establish a variable
>with an initial value of zero ?

Inconceivable!  (Sorry; I just read The Princess Bride again.)
*Three* FAQ list sneak-previews in one month!

89.  What can I safely assume about the initial values of variables
     which are not explicitly initialized?  If global variables start
     out as "zero," is that good enough for null pointers and floating-
     point zeroes?

A:   Variables with "static" duration (that is, those declared outside
     of functions, and those declared with the storage class static),
     are guaranteed initialized to zero, as if the programmer had typed
     "= 0".  Therefore, such variables are initialized to the null
     pointer if they are pointers, and to 0.0 if they are floating-
     point.  This requirement means that compilers and linkers on
     machines which use nonzero internal representations for null
     pointers and/or floating-point zeroes cannot necessarily make use
     of uninitialized, 0-filled memory, but must emit explicit
     initializers for these values (rather as if the programmer had).

     Variables with "automatic" duration (i.e. local variables without
     the static storage class) start out containing garbage, unless they
     are explicitly initialized.  Nothing useful can be predicted about
     the garbage.

     Dynamically-allocated memory obtained with malloc and realloc is
     also likely to contain garbage, and must be initialized by the
     calling program, as appropriate.  Memory obtained with calloc
     contains all-bits-0, but this is not necessarily useful for pointer
     or floating-point values.

                                            Steve Summit
                                            scs@adam.mit.edu

dave@cs.arizona.edu (Dave P. Schaumann) (02/26/91)

In article <1991Feb26.014801.29244@athena.mit.edu> scs@adam.mit.edu writes:
>In article <13704@hacgate.UUCP> wdelv@devnet3.UUCP (walt del vecchio) writes:
>>[what's the value of an uninitailized static var?]
>
>[static vars are 0, auto vars are random]

Interesting.  I'm just working out "Dave's law of variable non-useability":

	An uninitialized variable can be counted on to have a random
	value, until such time as a random value will be useful to
	the programmer.  Then, an uninitialized variable can be counted
	on to be zero.

(for the humor impaired, re-read with ";-)" mode on...)

-- 
Dave Schaumann      | Is this question undecidable?
dave@cs.arizona.edu |

scs@adam.mit.edu (Steve Summit) (02/26/91)

In article <957@caslon.cs.arizona.edu> dave@cs.arizona.edu (Dave P. Schaumann) writes:
>Interesting.  I'm just working out "Dave's law of variable non-useability":
>	An uninitialized variable can be counted on to have a random
>	value, until such time as a random value will be useful to
>	the programmer.  Then, an uninitialized variable can be counted
>	on to be zero.

Needs work.  It should read something like:

	An uninitialized value can be counted upon to have a
	non-random value, and in particular a non-random value
	which will make the program appear to work properly,
	until such time as all bugs are worked out, the original
	programmer's back is turned, and someone tries to port
	the code to another environment, assuming it will be
	"a simple matter of recompilation..."

                                            Steve Summit
                                            scs@adam.mit.edu