Scope of Variable in C++


Prev Tutorial Next Tutorial

Scope of Variable in C++

In C++ language, a variable can be either of global or local scope.

Global variable

Global variables are defined outside of all the functions, generally on top of the program. The global variables will hold their value throughout the life-time of your program.

Local variable

A local variable is declared within the body of a function or a block. Local variable only use within the function or block where it is declare.

Example of Global and Local variable

Example

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

int a;   // global variable
void main()
{
int b;    // local variable
a=10, b=20;
cout<<"Value of a: "<<a;
cout<<"Value of b: "<<b;
getch();
}

Output

Value of a: 10
Value of b: 20


Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements