Just above and below main()

Stan Brown browns at iccgcc.decnet.ab.com
Sat Apr 13 03:19:57 AEST 1991


In article <1243 at nanometrics.UUCP>, stealth at 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 at iccgcc.decnet.ab.com
My opinions are mine:  I don't speak for any other person or company.



More information about the Comp.lang.c mailing list