Naming a variable and specifying the data type that can be stored in it is accomplished using declaration statement. A declaration statement in C++ programs has the following syntax:
type name;
The type portion refers to the data type of the variable.
The data type determines the type of information that can be stored in the variable.
Example:
int sum;
long datenem;
double secnum;
Note:
- A variable must be declared before it can be used.
- Declaration statements can also be used to store an initial value into declared variables.
Example:
int num = 15;
float grade1 = 87.0;
Variable declarations are just the instructions that tell the compiler to allocate memory locations for the variables to be used in a program.
A variable declaration creates a memory location but it is undefined to start with, that means it's empty.
Example
#include <iostream.h>
int main()
{
float price1 = 85.5;
float price2 = 97.0;
float total, average;
total = price1 + price2;
average = total/2.0; // divide the total by 2.0
cout << "The average price is " << average << endl;
return 0;
}
The output of the above program:
The average price is 91.25
Let notice the two statements in the above program:
total = price1 + price2;
average = total/2.0;
Each of these statements is called an assignment statement because it tells the computer to assign (store) a value into a variable. Assignment statements always have an equal (=) sign and one variable name on the left of this sign. The value on the right of the equal sign is assigned to the variable on the left of the equal sign.






