Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ucbvax!husc6!diamond.bbn.com!mlandau From: mlandau@diamond.bbn.com.UUCP Newsgroups: comp.lang.c Subject: Re: default initialization of static and extern Message-ID: <6328@garnet.BBN.COM> Date: Wed, 3-Jun-87 23:34:52 EDT Article-I.D.: garnet.6328 Posted: Wed Jun 3 23:34:52 1987 Date-Received: Fri, 5-Jun-87 04:20:52 EDT References: <158@delftcc.UUCP> <1070@viper.Lynx.MN.ORG> <1297@genrad.UUCP> <675@gargoyle.UChicago.EDU> <10314@sri-spam.istc.sri.com> Reply-To: mlandau@Diamond.BBN.COM (Matt Landau) Organization: BBN Laboratories, Inc., Cambridge, MA Lines: 29 Keywords: C, NULL, portability Summary: static data is initialized only once In comp.lang.c (<10314@sri-spam.istc.sri.com>), robert@sri-spam.UUCP (Robert Allen) writes: > This may be a small point, but I always "assume" that static > variables local to a function are initialized to zero, and maintain > their value over time and function calls. If this not the case, > how can one effectively determine what to initialize to? If you > init explictly then it will be done each time you call the function, > so the routine isn't static. Nope. Static data -- even static data that is local to a function -- is initialized exactly once. If you have int counter() { static int value = 5; return value++; } then counter() will return 5 the first time it is called, 6 the next, and so on. The static variable "value" will NOT be reinitialized to 5 on each call. Incidentally, the initialization of static data usually happens at compile time, but there is no requirement that this be the case. (I know of one compiler that arranged for code to be generated to initialize all static data locations before calling main.) -- Matt Landau "Lead me not into temptation... mlandau@diamond.bbn.com I can find it myself."