Is this kosher?

Chris Torek chris at mimsy.umd.edu
Wed Dec 6 10:58:01 AEST 1989


In article <1047 at dinorah.wustl.edu> art at dinorah.wustl.edu (Arthur B. Smith)
writes:
>    listhd->str = "string literal";   /* This is the questionable line... */
 ...
>    As I RTFM, string literals have a static storage class, which
>means that they are guaranteed to have the same contents inside the
>block, but I am not clear on their linkage (which I think determines
>whether they have the same contents outside the block as well).

The following definitions may help:

  Scope - determines where the identifier's name may be used.  This is
  a matter for the compiler.  (Possibilities: block, file, function
  [labels only], prototype [prototypes only].)

  Linkage - basically the same as scope, but applies to the linker.
  (Possibilities: external [global], internal [file-wide but no more],
  none.)

  Duration (aka Lifetime) - determines when the contents of a variable
  are valid.  (Possibilities: static, automatic.)

For instance, a local array such as the one created by

	f() { char array[12]; ... }

has block scope (the name `array' vanishes at the close brace), has no
linkage (is effectively invisible to the linker), and has automatic
duration (lasts while the particular activation of f() is running).  To
change its duration to static, one declares it static:

	f() { static char array[12]; ... }

A string literal is an unnamed static array of char, and hence has:

  scope: none (there is no identifier, since it is unnamed)
  linkage: none
  duration: static

All static variables have static duration, i.e., are valid at all times
at which the program is running.  Some entities---global variables and
all functions---*always* have static duration.  The confusion comes in
here, as the `static' keyword can be applied to them, this time
changing not their duration (which is already static) but rather their
linkage.  Static globals get internal linkage, which means that their
names do not appear to exist outside the one file:

	int func() { ... }
	static int f1() { ... }
	/* func() can be seen outside, but f1() cannot */
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list