Variables are simply names used to refer to some location in memory – a location that holds a value with which we are working. Size of variables depends upon data type used to define it.
data-type variable-name ;
Rules for defining variables in C++
- The name of a variable can be composed of letters(a-z, A-Z), digits(0-9), and the underscore character(_).
- It must begin with either a letter or an underscore.
- Name is case-sensitive means Upper and lowercase letters are distinct.
- Blank spaces can't be used in variable names.
int EmployeeId; double Salary;
Assigning values to variables :
int EmployeeId = 10; double Salary = 10000.78;
The above line declares a variable names EmployeeId and assigns it a value 10.
The same objective can be achieved in following way:
int EmployeeId; EmployeeId = 10;
Understanding the above code :
|It defines a variable named EmployeeId and occupies a memory (2 bytes) in Computer Memory which can store an integer value.|
EmployeeId = 10;
|This is known as assignment statement. A value 10 is assigned to the variable EmployeeId. The left hand side of = operator (lvalue) must be a variable name while the rvalue can be another variable or some value.|