Just above and below main()

James Seymour jseymour at medar.com
Sun Apr 14 00:07:31 AEST 1991


In article <18473 at crdgw1.crd.ge.com> volpe at camelback.crd.ge.com (Christopher R Volpe) writes:
|In article <wolfram.671458575 at cip-s08>,
|wolfram at cip-s08.informatik.rwth-aachen.de (Wolfram Roesler) writes:
||>stealth at nanometrics.portal.UUCP (Steve Sabram) writes:
||>
||>>_________________________
||>
||>>int outside;
||>
||>>main()
||>>{
||>>int inside;
||>>...
||>>}
||>
||>>_________________________
||>
||>
||>>Our debate is which one of these 
||>>two are initialized to zero if
||>>any.
||>None of them is. 
|
|Um, no. outside is initialized to zero.
|
||>
||>For further info RTFM.
||>
|
|Good idea.
|

Very good idea.  Better yet, the manual specific to the compiler package
*you* are using.  On some older compilers, you needed to explicitly
initialize globals in the file they were intended to be *defined* in.  All
other occurances of things like the declaration of "outside" (as above)
were assumed to be a reference to an external that was defined in another
file.  If you didn't have a global initialized at least once, the symbol
for it was never defined, and thus the link phase would fail.  In newer
compilers however, declarations like that in the original question above
result in "outside" being global, it is defined in the file that contains
the above declaration (it is not preceded with the word "extern"), and the
space it occupies is initialized to zero.  Regardless of what newer
compilers do with globals however, IMHO it is best to explicitly initialize
such variables if you expect them to start with a known value - even 0.
Self-documenting code is a good practice.  Now, "inside" is local to the
function main and is an "auto" variable (it's space is on the stack), thus
it is un-initialized (will contain whatever was in the portion of memory
occupied by that variable when the stack frame is allocated at run-time).

-- 
Jim Seymour				| Medar, Inc.
...!uunet!medar!jseymour		| 38700 Grand River Ave.
jseymour at medar.com			| Farmington Hills, MI. 48331
CIS: 72730,1166  GEnie: jseymour	| FAX: (313)477-8897



More information about the Comp.lang.c mailing list