2.5.Variable Declaration in C++ #

There are two types of variable declaration, such as Local Variable and Global Variable.

  • Local Variables

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code.

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
	int radius=10;
	float area=radius*3.14;
	cout<<"Area of Circle: "<<area;
	getch();
}
  • Global Variables

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

Example:

#include<iostream.h>
#include<conio.h>
int i=2; 
void doubleValue() 
{ 
	i*=5; 
} 
void main() 
{ 
	cout<<"I: "<<i<<endl; 
	doubleValue(); 
	cout<<"Double I: "<<i<<endl; 
	getch(); 
}
Suggest Edit