Friday, July 29, 2016

C Basics - Lifetime, Scope, Visibility and Linkage in C

Lifetime is the period during execution of a program in which a variable or function exists. The storage duration of the identifier determines its lifetime. An identifier has global (static) or local (auto) lifetime:

An identifier with the storage-class static has static storage duration. Such identifiers have storage and a defined value for the duration of a program. Storage is reserved and the identifier's stored value is initialized only once, before program startup.

An identifier declared without the static storage-class has auto storage duration if it is declared inside a function. Such identifiers have storage and a defined value only within the block where the identifier is defined or declared. For auto storage class identifiers, new storage is allocated each time the program enters that block, and it loses its storage (and its value) when the program exits the block.

All functions have static lifetime.

Identifiers declared at the external level (that is, outside all blocks in the program at the same level of function definitions) always have global (static) lifetimes.

static storage class identifier within a block has global lifetime.

An identifier with a global lifetime exists throughout the execution of the source program.

If a local variable has an initializer, the variable is initialized each time it is created (unless it is declared as static). Function parameters also have local lifetime.

Scope and Visibility:
The scope of an identifier is the part of the program in which the name can be used. An identifier's visibility determines the portions of the program in which it can be referenced — its scope. The scope may be limited to the file, function, block, or function prototype in which an identifier appears.

File Scope: an identifier with file scope is accessible from any place in the translation unit after its declaration. Such identifiers are often called global or external.

Function Scope: A statement label is the only kind of identifier that has function scope.

Block Scope: an identifier with block scope appears inside a block or within the list of formal parameter declarations in a function definition. Such identifiers are sometimes called local variables.

Function-prototype scope: an identifier with function-prototype scope appears within the list of parameter declarations in a function prototype

Linkage:
A name's linkage affects whether two or more declarations for that name are valid, and if so, whether they refer to the same entity (such as a function or an object).

A file-scope identifier with storage-class-specifier static has internal linkage. Otherwise, the identifier has external linkage.

An identifier within a block does not include the extern storage-class specifier, the identifier has no linkage and is unique to the function.

The following identifiers have no linkage:
  • An identifier declared to be anything other than an object or a function
  • An identifier declared to be a function parameter
  • A block-scope identifier for an object declared without the extern storage-class specifier



Related topics:
Memory Management in C   |   Incomplete Type in C   |   Namespace in C   |   Complex and Abstract Declarations in C   |   Storage of Data Types in C   |   Standard Library in C

List of topics: C Programming

No comments:

Post a Comment