C++ is a strongly-typed language, all variables must be declared with their type before they can be used, as this instructs the compiler on the memory size to reserve for the variable and how to interpret its value
In C++, the syntax used in declaring a new variable is simple. It is shown below: firstly, you simply write the type followed by the variable name (i.e its identifier).
Syntax:
char c; //character variable declaration.
int area; //integer variable declaration.
float num; //float variable declaration.
Note: After you have declared the variables c, area, and num, they can be used within their scope in the remainder of the program.
In this case, when you want to declare more than one variable of the same type, you will declare them in a single statement by separating their identifiers with commas.
int a, b, c; //more than one variable declaration.
The syntax above declares three variables (a, b, and c), and they are of the type int and have the same meaning as this below:
int a; //integer variable declaration.
int b; //integer variable declaration.
int c; //integer variable declaration.
From the example above, when variables are declared, they have an undetermined or garbage value unless they are assigned a value at the beginning. However, it is possible for a variable to have a definite value at the moment it was declared, this process is referred to as the initialization of the variable. The C++ language presents the same ways to initialize variables as the C Language.
Syntax:
type identifier = initial_value;
Example:
int a = 10; //integer variable declaration & initialization.
Program Example:
//Write a CPP program for declaration & initialization of variable
#include <iostream.h>
int main ()
{
int sum; //Variable declaration
int a = 10; //Variable declaration & initialization
int b = 5; //Variable declaration & initialization
ans = a + b;
cout << "Addition is:" << ans << endl;
return 0;
}