Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think!ames!dftsrv!mimsy!chris From: chris@mimsy.umd.edu (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Is this kosher? Keywords: Does a static exist outside its block? Message-ID: <21122@mimsy.umd.edu> Date: 5 Dec 89 23:58:01 GMT References: <1047@dinorah.wustl.edu> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 53 In article <1047@dinorah.wustl.edu> art@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@cs.umd.edu Path: uunet!mimsy!chris