Image

C++ - Variable - Declaring Variable

Variable

Declaring Variable

As we know that, a variable is a named location in memory that is used to hold a value that may be modified by the program. All variables must be declared before they can be used.

We can declare variable in C++ anywhere in the scope just before the variable can in used.

Syntax:
	type variable_list;

Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier names separated by commas.

Here are some declarations:
int i,j,l;
short int si;
unsigned int ui;
double balance, profit, loss;
e.g
#include<conio.h>
#include<iostream.h>
void main()
{
 	int n;
 	clrscr();
	 int sum=0;
	 for(int i=0;i<5;i++)
	 {
	    cout<<"\n Enter no ";
	    cin>>n;
	    sum+=n;
 	 }
 float avg=sum/5.0;
 cout<<"\n sum = "<<sum<<" avg = "<<avg;
 getch();
}
O/P
Enter no 1
Enter no 2
Enter no 3
Enter no 4
Enter no 5
Sum = 15
Avg = 30