Thursday, Aug 16th

Last update12:59:40 PM GMT

What do you mean by scope and duration?

Write e-mail

Duration

argaiv1770

The duration of an object describes whether its storage is allocated once only, at program start-up, or is more transient in its nature, being allocated and freed as necessary.

There are two types of duration of objects: static duration and automatic duration. Static duration means that the object has its storage allocated permanently, automatic means that the storage is allocated and freed as necessary.

It's easy to tell which is which: you only get automatic duration if

  • The declaration is inside a function.
  • And the declaration does not contain the static or extern keywords.
  • And the declaration is not the declaration of a function.

Use these tables only as a guide to the simple cases

Storage Class Specifier Function or Data Object Linkage Duration
static either internal static
extern either probably external static
none function probably external static
none data object external static

                       Table 1: External Declarations (outside a function)

Storage Class Specifier Function or Data Object Linkage Duration
register data object only none automatic
auto data object only none automatic
static data object only none static
extern either probably external static
none data object none automatic
none function probably external static

                                   Table 2: Internal Declarations


Scope

The scope of the names of objects defines when and where a given name has a particular meaning. The different types of scopes are the following:

  • function scope
  • file scope
  • block scope
  • function prototype scope

Function scope only applies to labels, whose names are visible throughout the function where they are declared, irrespective of the block structure. No two labels in the same function may have the same name, but because the name only has function scope, the same name can be used for labels in every function. Labels are not objects; they have no storage associated with them and the concepts of linkage and duration have no meaning for them.

Any name declared outside a function has file scope, which means that the name is usable at any point from the declaration on to the end of the source code file containing the declaration. It is obviously possible for these names to be temporarily hidden by declarations within compound statements. As we know, function definitions must be outside other functions, so the name introduced by any function definition will always have the file scope.

A name declared inside a compound statement, or as a formal parameter to a function, has block scope and is usable up to the end of the associated } which closes the compound statement. Any declaration of a name within a compound statement hides any outer declaration of the same name until the end of the compound statement.

A special and rather trivial example of scope is function prototype scope where a declaration of a name extends only to the end of the function prototype. The scope of a name is completely independent of any storage class specifier that may be used in its declaration.


Share this post



Web Hosting