Scoping question in C

G. Matthew Rice matt-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org
Thu Jun 9 23:36:36 UTC 2005


Henry Spencer <henry-lqW1N6Cllo0sV2N9l4h3zg at public.gmane.org> writes:
> On Thu, 9 Jun 2005, William Park wrote:
> > 	x = "something";
> > 	return (1);
> > where 'x' is global variable being used elsewhere.  Can I use 'x' after
> > func() exits?  That is, is 'x' still pointing to string "something"?
> 
> Yes.  This isn't about *scope*, which is where things are visible, but
> about storage duration, which is how long things live.  String constants
> have static storage duration -- they last the lifetime of the program.

Along the scope lines, though, if you want to access x from another file, you
need to define it in that other file (or an include, ...) with extern.  If
you want to restrict its use to only the file with the function func, you
need to declare it static.

I just found a reasonable description of this at:

    http://www.its.strath.ac.uk/courses/c/subsection3_6_3.html#SECTION0006300000000000000

read the global, external and static pages.  They're short.

HTH,
-- 
g. matthew rice <matt-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>           starnix, toronto, ontario, ca
phone: 647.722.5301 x242                                  gpg id: EF9AAD20
http://www.starnix.com              professional linux services & products
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list