Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!tuvie!vmars!hp From: hp@vmars.tuwien.ac.at (Peter Holzer) Newsgroups: comp.lang.c Subject: Re: Scope of 'static' variables Message-ID: <2175@tuvie> Date: 26 Nov 90 16:56:07 GMT References: <7744.27500de8@jetson.uh.edu> Sender: news@tuvie Lines: 33 elee6i5@jetson.uh.edu writes: >When you use 'static' to initialize an array in side a fn, >as in static int data[3] = {1,2,3}; the array is initialized >before the program runs. Since this can't be allocated on the >stack frame, it has to be allocated on the data segment(?). >But how is 'scope' preserved for such static variables >declared inside a function ? The variable is only visible inside the fuinction it is defined (exactly: inside the block it is defined) but it exists even when the function is not active (so you can pass a pointer to it to the calling function which can manipulate it). >Could any one explain this ,with reference to ix86 architecture? The static variables usually get names which cannot clash with other variables (something like L0001 (or just BeginDataSegmentOfThisModule + Offset), C external variables are prefixed with an underscore), and are not exported to other modules. So static variables are just like globals but no other function knows its name, so it cannot be referenced (except through a pointer). Turbo C does it this way, and all other compilers I know do it similarly. -- | _ | Peter J. Holzer | Think of it | | |_|_) | Technical University Vienna | as evolution | | | | | Dept. for Real-Time Systems | in action! | | __/ | hp@vmars.tuwien.ac.at | Tony Rand |