Scope Rules in C++

The portion of the program where an identifier can be used is known as its scope

For example, when we declared a local variable in a block, it can be referenced only in that block and in blocks nested within that block. An identifier declared outside any function has to file scope.Such an identifier is “known” in all functions from the point at which the identifier is declared until the end of the file.Global variables, function definitions and function prototypes placed outside a function all have file scope.
Labels(identifiers followed by a colon such as start:) are the only identifiers with function scope.Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body.Labels are used in switch structures(as case labels) and in goto statements.labels are implementation details that functions hide from one another.This hiding- more formally called information hiding is one of the most fundamental principles of good software engineering.
Identifiers declared inside a block haveblock scope.Block scope begins at the identifier’s declaration and ends at the terminating right brace ( } ) of the block in which the identifier is declared.Local variables have block scope, as do function parameters, which are also local variables of the function.Any block can contain variable declarations.When blocks are nested and an identifier in an outer block has the same name as an identifier in an inner block, the identifier in the outer block has the same name as an identifier in an inner block, the identifier in the outer block is “hidden” until the inner block terminates.While executing in the inner block, the inner block sees the value of its own local identifier and not the value of the identically named identifier in the enclosing block.Local variablesdeclared static still havve block scope, even though they exist from the the time the program begins execution.Storage duration does not affect the scope of an identifier.
The only identifiers with function-prototype scope are those used in the parameter list of a function prototype.As mentioned previously, function prototypes do not require names in the parameter list – only types are required.If a name appears in the parameter list of a function prototype, the compiler ignores the name.Identifiers used in a function prototype can be reused elsewhere in the program without ambiguity.in a single prototype, a particular identifier can be used only once.

Accidentally using the same name for an identifier in an inner block that is used for an identifier in an outer block, when in fact the programmer wants the identifier in the outer block to be active for the duration of the inner block is normally a logic error.

Avoid variable names that hide names in the outer scope. This can be accomplished by avoiding the use of duplicate identifiers in a program

Program of Scope Rule in C++

#include<iostream>
using std::cout;
using std::endl;
void useLocal( void );
void useStaticLocal( void );
void useGlobal( void );
int x=1;
int main()
{
int x=5;
cout<<"local x in main's outer scope is"<<x<<endl;
{
int x=7;
cout<"local x in main's inner scope is"<<x<<endl;
}
cout<<"local x in main's outer scope is"<<x<<endl;
useLocal();
useStaticLocal();
useGlobal();
useLocal();
useStaticLocal();
useGlobal();
cout<<"\n local x in main is"<<x<<endl;
return 0;
}
void useLocal( void )
{
int x=25;
cout<<endl<<"local x is"<<x
    <<"on entering useLocal"<<endl;
++x;
cout<<"local x is"<<x
    <<"on exiting useLocal"<<endl;
}
void useStaticLocal( void )
{
static int x=50;
cout<<endl<<"local static x is"<<x
    <<"on entering useStaticLocal"<<endl;
}
void useGlobal( void )
{
cout<<endl<<"global x is"<<x
    <<"on entering useGlobal"<<endl;
x*=10;
cout<<"global x is"<<x
    <<"on exiting useGlobal"<<endl;
}

OUTPUT:

local x in main's outer scope is 5
local x in main's inner scope is 7
local x in msin's outer scope is 5

local x is 25 on entering useLocal
local x is 26 on entering useLocal

local static x is 50 on entering useStaticLocal
local static x is 51 on entering useStaticLocal

global x is 1 on entering useGlobal
global x is 10 on entering useGlobal

local x is 25 on entering useLocal
local x is 26 on entering useLocal

local static x is 51 on entering useStaticLocal
local static x is 52 on exiting useStaticLocal

global x is 10 on entering useGlobal
global x is 100 on entering useGlobal

local x in main is 5  

Function useStaticLocal declares static variable x and initializes it to 50. Local variables declared as static retain their values even when they are out of scope(i.e. the function in which they are defined is not executing). When the program calls useStaticLocal, the function prints x increments it and prints it again before the function returns program control to its caller. In the next call to this function, static local variable x contains the value 51

Function use global does not declare any variables. Therefore, when it refers to variable x, the global x is used. When the program calls useGlobal, the function prints the global variable x, multiples it by 10 and prints again before the function returns program control to its caller. The next time the program calls useGlobal, the global variable has its modified value, 10. After executing function useLocal,useStaticLocal and useGlobal twice each, the program prints the local variable x in main again to show that none of the function calls modified the value of x in main because the functions all referred to variables in other scopes.

Translate »