Overview:

This post explains concepts of Scope,Duration and Linkage in C programming language.It explains how scope, duration and linkage are determined for each function and variable depending upon its storage class specifier and its location in the file(source file).We will use ‘object’ for “function or variable” and when we say “declaration” it always mean “object” declaration.

INTRODUCTION:

SCOPE:

Scope of declaration is the part of the code where declaration can be seen and used.

DURATION:

Every object have a property ‘storage duration’ which limits its lifetime.Duration actually indicates whether object associated with the declaration present throughout the program execution or it is dynamically allocated when the declaration scope is entered.

LINKAGE:

Linkage determines multiple declaration of the same object refers to the same object or different.

As we have seen the above briefly explained topic are inter related .We are going to see them in Detail.

SCOPE:

There are four scopes featured by C programming language.

1.Block scope

2.Function scope

3.Function prototype scope

4.File scope

Block Scope:Every  variable or function declaration have a block scope.The scope goes from the place of declaration to the end of that block.. Braces {} are blocks and variables declared inside them have block scope.

Function Scope: function Scope is the scope of the Goto Lables  .Goto Labels are implicitly declared  where they appears but they are visible through out the function even if they appear inside the block.Here is the key point to know that goto labels and objects have different namespace so we can have that same name for goto labels and object.

Function Prototype Scope:A  name that is in the parameter list of a function prototype has function prototype scope. This basically means that the scope is from the point it is declared till the closing parenthesis of the function prototype.

File Scope:All variables and function that are declared outside the function have File scope.They are visible from the point of declaration to the end of file.Here by ‘file’ we mean that source file being compiled.

STORAGE CLASS SPECIFIERS:

There are five types of Storage class specifier.

1.auto: automatic duration and no linkage

2.register: same as auto but address of this variable cannot be taken

3.static: static duration and internal linkage (unless it is block scope)

4.extern: static duration and external linkage(unless it is already declared internal)

auto:The auto access specifier is only allowed for the object declared inside the block(except for the function parameter list).It indicate automatic storage duration and no linkage.

register :The register access specifier is only allowed for the object declared inside the block including for the function parameter list.It indcate automatic storage duration and no linkage., variables declared register cannot be used as arguments to the address-of operator  and register arrays are not convertible to pointers.

static :The static specifier specifies both static duration and internal linkage.It can be used with function at file scope and with variables at both file and block scope.It cannot be used with function parameter list.

extern :he static specifier specifies both static duration and external linkage.It can be used  at file scope and block scope.

If extern appears for the redeclaration of the identifier which was already declared with the internal linkage ,then the linkage remains internal otherwise it is external.

PS:

If no storage-class specifier is provided, the defaults are:

extern for all functions
extern for objects at file scope
auto for objects at block scope.

DURATION:

There are two kinds of duration:

1.static

2.automatic

static: A static duration means the object associated to the declaration persists for the whole program execution.

automatic: Automatic duration means the object is allocated dynamically when the declaration’s scope is entered

Within functions (ie block-scope), declarations without extern or static have automatic duration. Any other declaration (ie file or block-scope) has a static duration.

Linkage:

Linkage refers to the ability of an identifier (variable or function) to be referred to in other scopes. If a variable or function with the same identifier is declared in several scopes, but cannot be referred to from all of them, then several instances of the variable are generated. The following linkages are recognized:

1.no linkage: The identifier can be referred to only from the scope it is in. All function parameters and all non-extern block-scope variables (including the ones declared static) have this linkage.

2.internal linkage: The identifier can be referred to from all scopes in the current translation unit. All static identifiers (both functions and variables) have this linkage.

3.external linkage: The identifier can be referred to from any other translation units in the entire program. All non-static functions, all extern variables (unless earlier declared static), and all file-scope non-static variables have this linkage

Advertisements