A variable is nothing but a name given to a storage area that our programs can manipulate. This location is used to hold the value of the variable, which determines the size and layout of the variable’s memory.
Following are the rules for Naming C++ Variables:
- Variable name must begin with a letter or underscore.
- They are case sensitive.
- They can be constructed with digits, letters.
- No special symbols are allowed other than underscore.
Declaring and Initializing a variable:
Declaration of variables must be done before they are used in the program.
Declaration does the following things.
- It tells the compiler what the variable name is.
- It specifies what type of data the variable will hold.
int a; // declared but not initialised
Initializing a variable means to provide it with a value. A variable can be initialized and defined in a single statement, like:
a = 20 // initialised
Let’s write a program in which we will use some variables.
int main ()
// Variable declaration
int a, b, c;
/* actual initialization */
a = 8;
b = 2;
/* using addition operator */
c = a + b;
/* display the result */
cout<<"Sum is:"<< c ;
Sum is : 10