Google News
logo
CPP - Interview Questions
Comment on Local and Global scope of a variable.
The scope of a variable is defined as the extent of the program code within which the variable remains active i.e. it can be declared, defined or worked with.
 
There are two types of scope in C++ :
 
Local Scope : A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block.

Global Scope : A variable has a global scope when it is accessible throughout the program. A global variable is declared on top of the program before all the function definitions.

Example :
#include <iostream.h>
Int globalResult=0; //global variable
int main()
{
Int localVar = 10; //local variable.
….. 
}
Advertisement