Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!rutgers!umd5!mimsy!chris
From: chris@mimsy.UUCP (Chris Torek)
Newsgroups: comp.lang.c
Subject: Re: scoping vs linking
Message-ID: <9563@mimsy.UUCP>
Date: Sun, 29-Nov-87 23:27:42 EST
Article-I.D.: mimsy.9563
Posted: Sun Nov 29 23:27:42 1987
Date-Received: Wed, 2-Dec-87 22:52:32 EST
References: <10575@brl-adm.ARPA>
Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742
Lines: 61

In article <10575@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU writes:
>I am very puzzled by the scoping rules in C.

It is quite simple, actually.  The scope of a variable is the
level at which it was declared, with the restriction that anything
declared `static' cannot be directly referenced from outside the
current file.  In other words:

	int global;		/* scope is global, but code in other files 
				   must use `extern int global' to reference
				   this */
	static int filelocal;	/* code in other files cannot reference
				   this at all */

	f()			/* f is global */
	{
		int v;		/* local to f */
		static int s;	/* local to f and other files cannot
				   reference it, not that they could
				   even without `static'. */
		...
	}

	static
	g()			/* g is local to this file */
	{
		...
	}

While static thus has two meanings (`make it static' and `keep it
to this file'), they never conflict.

>I've been looking at some code which is spread out over several
>files and which compiles and runs fine on various systems, including
>the SUN.

[example deleted due to length; see the parent article]

>Everything works fine.

Then there is something that you omitted from your example;
as it was, it was illegal and would not have compiled on my Sun.

>Is there a way to tell routine2 that klugev is the variable introduced
>in routine1? The scoping rules don't seem to permit this.

That is correct: they do not.  That is, there is no direct analogue
to the Pascal construct

	procedure p;
	var pvar : integer;
		procedure q;
		begin pvar := 4 end;
	begin q end;

There are efficiency reasons not to implement such scoping, and
it is never necessary: it is always possible to pass the shared
variable from p to q.  It may be convenient at times, but C does
not provide it.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris