[comp.lang.c] Scope of 'static' variables

elee6i5@jetson.uh.edu (11/26/90)

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 ?

Could any one explain this ,with reference to  ix86 architecture?

.sundar
elee6i5@jetson.uh.edu

hp@vmars.tuwien.ac.at (Peter Holzer) (11/27/90)

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 |

henry@zoo.toronto.edu (Henry Spencer) (11/27/90)

In article <7744.27500de8@jetson.uh.edu> elee6i5@jetson.uh.edu writes:
>But how is 'scope' preserved for such static variables 
>declared inside a function ?

Easily.  The compiler compiles code exactly as if you had declared the
variable outside the function.  The only thing it does differently is
to keep an eye on where you are using the variable, and forbid use of
it outside that function.
-- 
"I'm not sure it's possible            | Henry Spencer at U of Toronto Zoology
to explain how X works."               |  henry@zoo.toronto.edu   utzoo!henry

dstailey@gnu.ai.mit.edu (Doug Stailey) (12/07/90)

In article <2175@tuvie> hp@vmars.tuwien.ac.at (Peter Holzer) writes:
>elee6i5@jetson.uh.edu writes:
>
>
>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.
>--

But what if you want to call a function returning a static from another
source module?  Say for instance, in main.c you have a function declared:
static char *get_cmd(FILE *fp)
and you want to call it from misc.c.  You can't say
extern static char *get_cmd(FILE *fp)
since most compilers will respond with the "too many storage classes" error.
If I declare it: extern char *get_cmd(FILE *fp) this gives a warning
under GCC.  I'm not sure whether it works with Turbo C.  How should
such a function be declared externally?


--
disclaimer: This message is sold by weight, not volume.  Contents may have
	    settled during shipment.

epames@eos.ericsson.se (Michael Salmon) (12/07/90)

In article <1990Dec7.051955.2883@mintaka.lcs.mit.edu> Doug Stailey writes:
>But what if you want to call a function returning a static from another
>source module?  Say for instance, in main.c you have a function declared:
>static char *get_cmd(FILE *fp)
>and you want to call it from misc.c.  You can't say
>extern static char *get_cmd(FILE *fp)
>since most compilers will respond with the "too many storage classes" error.
>If I declare it: extern char *get_cmd(FILE *fp) this gives a warning
>under GCC.  I'm not sure whether it works with Turbo C.  How should
>such a function be declared externally?

Static has no meaning when used to describe a return value, instead
what you are doing is declaring that the function is local to the file
in which it is declared. If you just delete the static from the
declaration then you can do what you want. Some authors of C books
advocate #defining private to be static and that makes the declaration
more obvious although I personally believe that making C look like
something else is not particularly desireable.

Michael Salmon
L.M.Ericsson
Stockholm

gwyn@smoke.brl.mil (Doug Gwyn) (12/08/90)

In article <1990Dec7.051955.2883@mintaka.lcs.mit.edu> dstailey@gnu.ai.mit.edu (Doug Stailey) writes:
>But what if you want to call a function returning a static from another
>source module?  Say for instance, in main.c you have a function declared:
>static char *get_cmd(FILE *fp)

"static" applies to the function; it has NOTHING to do with the value
returned.  Thus "function returning a static" is incorrect terminology.
"The function is statically defined" would be acceptable.

	extern char *example(void);

	char *example(void) {
		static char buffer[100];
		/*...*/
		return buffer;
	}

kaleb@thyme.jpl.nasa.gov (Kaleb Keithley ) (12/08/90)

In article <14680@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <1990Dec7.051955.2883@mintaka.lcs.mit.edu> dstailey@gnu.ai.mit.edu (Doug Stailey) writes:
>>But what if you want to call a function returning a static from another
>>source module?  Say for instance, in main.c you have a function declared:
>>static char *get_cmd(FILE *fp)
>
>"static" applies to the function; it has NOTHING to do with the value
>returned.  Thus "function returning a static" is incorrect terminology.
>"The function is statically defined" would be acceptable.
>
>	extern char *example(void);
>
>	char *example(void) {
>		static char buffer[100];
>		/*...*/
>		return buffer;
>	}

To (perhaps) clarify.  A static function is not "public", i.e. you may
not call a static function from another module; it is invisible to all
but file it is in.  By default, all C functions are "public" unless
explicity declared "private" with the static keyword.

-- 
Kaleb Keithley                      Jet Propulsion Labs
kaleb@thyme.jpl.nasa.gov

You can please all of the people some of the time,