Variables are anything such as persons name, address, number etc.
Definition – Variables in C++ is the name given to the memory location.
Any data in C++ are stored into the memory and we can identify that location by its name that is we address that memory location is referred as Variables.
Usually in C++, We first declared the Variables.
Value of variable changes during execution of program.
Syntax for declaring variable
<Data type> <variable name>;
In syntax, we first write data type and write name of variable and with semicolon.
Example :- int no;
In above example, we can see first we write data type of variable and write name given to variable and end with semicolon.
Rules for variable declaration
- Variable name must start with alphabet or underscore.
- Variable name can not start with number or any special symbol.
- No space is allowed in between the variable name declaration.
- No special character is allowed.
Constant variable in c++
- constant variable are defined as varible whose value can not be changed.
- The value of variable can not be changed.
- We can define these variable using const keyword.
const <Data type> <variable name> = value;
const int no = 10;
In above example we write const keyword to declare constant variable of integer data type and store value 10. This value can’t be changed during entire program.
Program to demonstrate variable in c++
void main ()
int no; //declare int variable
const int pi = 3.14 ; // declare constant variable
char c;. //declare char variable
no = 10;
Integer variable = 10
Constant variable = 3.14
Character variable = s
- In above program we demonstrate variable in c++
- First we declare the variable of int and character type and one constant variable and defined value in variable
- Print variable