2.4 Visibility

An identifier is visible only within a certain region of the program. An identifier has visibility over its entire scope, unless a subsequent declaration of the same identifier in an enclosed block overrides, or hides, the previous declaration. Visibility affects the ability to access a data object or other identifier, because an identifier can be used only where it is visible.

Once an identifier is used for a specific purpose, it cannot be used for another purpose within the same scope, unless the second use of the identifier is in a different name space. Section 2.14 describes the name space restrictions. For example, declarations of two different data objects using the same name as an identifier is illegal within the same scope.

When the scope of one of two identical identifiers is contained within the other (nested), the identifier with inner scope remains visible, while the identifier with wider scope becomes hidden for the duration of the inner identifier's scope.

In the following example, the identifier number is used twice: once as an integer variable and once as a floating- point variable. For the duration of the function main , the integer number is hidden by the floating-point number .

#include <math.h>
int number;        /*  number is declared as an integer variable  */

main ()
{
 float x;
 float number;     /*  This declaration of number occurs in an inner
                       block, and "hides" the outer declaration.
                       The inner declaration creates a new object */
 x = sqrt (number);/*  x receives a floating-point value          */
}


Previous Page | Next Page | Table of Contents | Index