What is a variable in C++ | Declaration of variable in C++ | Rules for naming variable in C++
What is a variable in C++?
A variable is the name given to some memory location . Variable can change its value during the execution of a C++ program.
Rules to define a variable in C++
– Alphabets and digits both can be used.
– Variable must start with an alphabet or underscore.
– Digits can appear only after first character.
– No special character except underscore can be used.
– Keywords can’t be used to declare variables.
– Spaces are not allowed.
– Capital letters and small letters are considered different.
Examples of valid variable names:
Studentname, father_name , rank1 , distance
Examples of invalid variable names:
1st [ First character is a digit ]
float [ float is a keyword ]
$500 [$ is a special character ]
student name [Space not allowed ]
Declaration of variable in C++
The syntax for declaring a variable in C++ is
<type> v1,v2,v3,……….;
type is any valid data type of C++.
v1, v2, v3 are variables names..
Examples
int a,b;
float sal;
Initialization of variables in C++
Initialization is to to assign some value to the variable during declaration.
Example
int a=1,b=2; // Two variables a and b are initialized with values 1 and 2.