Wednesday, November 15, 2023

Variable Scope

Scope means the accessibility and visibility of a variable at different points in the program. Variable have their own boundaries where those are accessible, outside those boundaries variable don’t hold their values.

In C++, variable has four types of scopes. 

Block, Function, file and Program Scope.

1) Block Scope:

Block means a statement block, is a group of statements enclosed with an opening & closing curly brackets ( {,} ). Variables defined inside a block are called local variables. Local variables have automatic duration, which means they are created (and possibly initialized) at the point of declaration, and destroyed when the block they are defined in is exited. Local variables have block scope (also called local scope), which means they enter scope at the point of declaration and go out of scope at the end of the block that they are defined in.

Example:

void main()

{

int a = 0;          // scope of the first 'a' begins

++a;           // the name 'a' is in scope and refers to the first 'a'              

Cout<<”a = “<<a;     // output a=1                                                      

{                        // scope of the second 'a' begins                                  

int a = 20;          // scope of the first 'a' is interrupted                           

Cout<<”a = “<<a;     // output a=20 because second a is initialized to 20     

a = 42;                       // 'a' is in scope and refers to the second 'a'        

Cout<<”a = “<<a;  //output a=42 because second a is reinitialized to 40     

}                                // block ends, scope of the second 'a' ends          

Cout<<”a = “<<a;     //scope of the first 'a' resumes so where a=1      

}                        // block ends, scope of the first 'a' ends                       

2)  Function Scope:

The variable declared inside function has function scope i.e. they can be accessed only in function that declares them. From outside the function these variables are not accessible. Variables defined inside a function are called local variables.

#include<iostream.h> #include<conio.h>

void test();

void main()

{

int a=10;          //local variable to main()

cout<<"a= "<<a;

test();

b=10;               //illegal, b not visible inside main(),

}

void test()

{

int b=20;

cout<<"b= "<<b;

cout<<"a= "<<a;         //illegal, b not visible inside //main()

}

3) Program Scope:

Global variable have Program scope (also called global scope), if a variable is defined outside any function, then that variable is called a global variable. Any part of program after global variable declaration can access global variable. If a global variable is defined at the beginning of the listing, global variable is visible to all functions.

Example:

#include<iostream.h>                                            

int g=10;                             

void test();                                                             

void main()

{                                          

g++;                                                                       

cout<<"g at main() is = "<<g<<"\n"; test();

}

void test()

{

g++;

cout<<"g at test() is = "<<g<<"\n";

}

Variable ‘g’ is a global variable; hence it can be accessed from any function in the program. Initial value of g=10.

Global & Local Variable NAME CONFLICT.

#include<iostream.h>

#include<conio.h>

int g=10; //global variable

void display( );

void main( )

{

clrscr();

cout<<"\n the value of g in the main()= "<<g;

display();

getch();

}

void display( )

{

int g=20; //local variable

cout<<"\n the value of g in the display()= "<<g;

}

If we have a variable declared (local variable) in a function that has the same name as that of the global variable declared, the function will use the local variable declared with in it and ignore the global variable.

4) File Scope:

When a global variable is accessible until the end of the file, the variable is said to have file scope. To allow a variable to have file scope declare that variable with the ‘Static’ keyword, before specifying its data type as follows.

Static int x=10;

A global static variable can be used anywhere from the file in which it is declared but it is not accessible by any other files. Such variable are useful when the programmer writes his own header files.

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top