Forward Referencing of Static Variables OK?

Peter da Silva peter at graffiti.UUCP
Sun Oct 13 06:59:53 AEST 1985


Answer: no. At least not according to the available Ritchie documentation.

> The orginal article asked what should happen in this sort of case:
> 
> /***********************************/
> extern int xxx;
> 
> f(){
> 	xxx = 1;
> }
> 
> static int xxx;	/* `extra' information about xxx */
> /***********************************/
> 
> The ANSI committee had a lot of work to do on this one.

Why? It's perfectly obvious.

The scope of a "global scope" declaration is the entire file after the
declaration. It is not known before the declaration. If the extern keyword
is used, storage is assumed to be allocated elsewhere. If the static
keyword is used, the name is local to the file (useful when building
libraries). The above code is not correct 'C', since xxxx is redeclared.

> There is an even worse issue than that:
> 
> /***********************************/
> f(){
> 	{
> 	extern int xxx;	/* OH BOY!!! - where am I visible? */
> 			/* Am I really extern? */
> 	}
> }
> 
> y(){
> 	/* can I use xxx in here ???? */
> }
> /***********************************/

No. The scope of a declaration within a block is the remainder of the block.
xxx is known within the block, nowhere else.

I don't know about Plauger, but this is all explained in various ancillary
documents to the UNIX programmer's manual.



More information about the Comp.lang.c mailing list