keyword modifier on an object in a C program.

Team-FLY

A.5 Identifiers, Storage Classes and Linkage Classes

Programmers are often confused about the meaning of the keyword static , in part because C uses the word two different ways. The main points to remember are the following.

  1. If static is applied to a function, that function can only be called from the file in which it is defined.

  2. If a variable definition appears outside any block, the variable exists for the duration of the program. If static is applied, the variable can only be accessed from within the file containing the definition. Otherwise, the variable can be accessed anywhere in the program.

  3. If a variable is defined inside a block, it can only be accessed within the block. If static is applied, the variable exists for the duration of the program and it retains its value when execution leaves the block. Otherwise, the variable is created when the block is entered, and it is destroyed when execution leaves the block. Such a variable needs to be explicitly initialized before it can be used.

These rules are based on C's notion of scope of an identifier and linkage , which we now discuss.

According to the ISO C standard, "An identifier can denote an object; a function; a tag or member of a structure, union, or enumeration; a typedef name; a label name; a macro name ; or a macro parameter." [56, section 6.2.1] Here, we mainly discuss identifiers that are associated with variables and functions.

An identifier can be used only in a region of program text called its scope . If two different entities are designated by the same identifier, their scopes must be disjoint , or one scope must be completely contained in the other. In the inner scope, the other entity is hidden and cannot be referenced by that identifier.

The scope begins at the identifier declaration. If the declaration occurs inside a block, the identifier has block scope and the scope ends at the end of the block. If the declaration occurs outside any block, the identifier has file scope , and the scope ends at the end of the file in which it is declared.

Identifiers declared more than once may refer to the same object because of linkage. Each identifier has a linkage class of external, internal or none. Declarations in a program of a particular identifier with external linkage refer to the same entity. Declarations in a file of a particular identifier with internal linkage represent the same entity. Each declaration of an identifier with no linkage represents a unique entity.

An identifier representing a function has external linkage by default. This means that it can be referenced in any file of the program. Referencing it in a file other than the one in which it is defined requires a function prototype. You can hide a function from other files by giving it internal linkage, using the static qualifier.

An identifier representing an object (such as a variable) has a linkage class related to its storage class , also called storage duration . The storage duration determines the lifetime of the object, the portion of the program execution for which storage is guaranteed to be reserved for the object. There are three storage durations: static, automatic and allocated. Allocated objects have a lifetime that begins with a successful malloc or related function and ends when the object is explicitly freed or the program terminates. The lifetimes of other objects are determined by the declaration of the corresponding identifier.

An identifier of an object declared outside any block has static storage class. Objects with static storage class have a lifetime that is the duration of the program. They are initialized once and retain their last stored value. If no explicit initialization is given in the declaration, they are initialized to 0. As with functions, these identifiers have external linkage by default but can be given internal linkage by means of the static qualifier.

An identifier of an object declared inside a block has no linkage. Each identifier denotes a unique object. These identifiers have automatic storage duration by default. Objects with automatic storage class have a lifetime that begins when execution enters the block and ends when execution exits the block. These objects are not initialized by default and do not necessarily retain their last stored value after execution exits the block. If the block is entered through recursion or with multiple threads, each entry into the block creates a distinct object. A variable with automatic storage class is called an automatic variable.

An identifier of an object declared inside a block can be given static storage duration with the static qualifier. The object then has a lifetime that is the duration of the program and retains its last stored value. If the block is entered through recursion or multiple threads, the same object is used.

Objects with identifiers having static storage duration are often called static variables; those with identifiers having automatic storage duration are called automatic variables.

As described above, the static qualifier can affect either the storage class or linkage class of an object depending on the context. When static is applied to a function, it always changes its linkage class from the default of external to internal. Functions do not have a storage duration. For objects declared inside a block, the linkage class is always none and static changes the storage class from automatic to static. For objects declared outside any block, the storage class is always static and the static specifier changes the linkage class from external to internal. These rules are summarized in Table A.3.

Table A.3. Effect of using the static keyword modifier on an object in a C program.

where declared

static modifies

static applied?

storage class

linkage class

inside a block

storage class

yes

static

none

inside a block

storage class

no

automatic

none

outside any block

linkage class

yes

static

internal

outside any block

linkage class

no

static

external

Team-FLY


Unix Systems Programming
UNIX Systems Programming: Communication, Concurrency and Threads
ISBN: 0130424110
EAN: 2147483647
Year: 2003
Pages: 274

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net