Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!decwrl!ucbvax!agate!usenet.ins.cwru.edu!abvax!iccgcc!browns From: browns@iccgcc.decnet.ab.com (Stan Brown) Newsgroups: comp.lang.c Subject: Re: Just above and below main() Message-ID: <4237.2805a5ed@iccgcc.decnet.ab.com> Date: 12 Apr 91 17:19:57 GMT References: <1243@nanometrics.UUCP> Lines: 48 In article <1243@nanometrics.UUCP>, stealth@nanometrics.portal.UUCP (Steve Sabram) writes: > Ok, we got a big debate here at work about > the folowing piece of code; > _________________________ > > int outside; > > main() > { > int inside; > .... > } > > _________________________ > > Just what are the differences between these two variables? > > We all agree that "outside" is a global and thus accessable > to all functions in this file while "inside" is accessable > only to everything in main(). "outside" is also accessible to any function in any other source file if the function (or the source file) contains "extern int outside;" > > Our debate is which one of these two are initialized to zero if > any. This is important to us since we are coding for > a microcontroller and ROMing the code. We are detecting > cold or warm resets with the varable "outside". We just > want to make sure if this is just to this complier or a part of ANSI. Sec 3.5.7 of the ANSI standard: "If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 ...." So "outside" is initialized to 0 and "inside" is not. ("Static storage duration" covers more than just variables declared static.) However, if you're expecting external events to change "outside" then you need to declare it volatile and you have no guarantee that the program's initialization is the most recent store to the variable by the time the first line of your main function is executed. (See sec 3.1.2.4). Stan Brown, Oak Road Systems, Cleveland, Ohio, USA +1 216 371 0043 email (until 91/4/30): browns@iccgcc.decnet.ab.com My opinions are mine: I don't speak for any other person or company.