Forward Referencing of Static Variables OK?

Oleg Kiselev x268 oleg at birtch.UUCP
Wed Oct 16 15:40:53 AEST 1985


> From the way I read K&R, the "static" keyword is redundant, unless
> it is inside a function.  In other words, "static int thing;" is
> equivalent to "int thing;" unless the declaration takes place inside
> a function.  (Any comment on that??)
> 
 Yes! "static" means that only the file where the "static <whatever> thing"
 is declared will know about it! It's usefull for doing data hiding and modular
 a la Ada developement ( Thanks to Dan Berry for teaching how to do it right!)

> For instance, if file a.h has the
> declaration "int thing;", and a.h is included by b.c and c.c, does
> b.c access the same "thing" that c.c does?  I don't think that K&R
> specify.  And if the declaration is "extern int thing" then there must
> still be a further declaration of "thing" somewhere else.  (Anybody
> got anything else to say???)

As a matter  of fact!
Yes they do. The linker takes care of resolving this type of reference. If you 
have ever written a linker you'd know!

As long as we are at it : if a.h declares "static <whatever> thing" and is
included by b.c and c.c, b.c and c.c see different things.

And if b.c declares "static <> thing;" and c.c "extern <> thing" linker will
complain about undefined "_c" in c.c!

Also, if you have "extern <> thing" and "static <> thing" static declaration
supercedes the external and is the one seen in the file.

And equivalent "extern" and "auto" declarations are mapped into the same 
location.

And, yes, declaring the same variable two different types is a no-no in the same
file.
> 				Lowell Savage
-- 
-----------------------------------+ With deep indifference,
"I disbelieve an army of invisible |                       Oleg Kiselev.
 mind-flayers!"                    | DISCLAIMER:
"OK. They are *still* not there."  | I don't know what I am talking about and 
-----------------------------------+ therefore am not responsible for any
                                     damages to people who take me seriously!
...!trwrb!felix!birtch!oleg          
...!{ihnp4|randvax}!ucla-cs!uclapic!oac6!oleg


Nothing I ever say reflects the views or opinions of my employers.
They knew who they hired though!



More information about the Comp.lang.c mailing list