Variables
A variable is a space in a computer’s memory set aside for a certain kind of data and given a name for easy reference.or A variable is the name used for the quantities which are manipulated by a computer program. Variables are used so that the same memory space in memory can hold different values at different times.
In order to distinguish between different variables, they must be given identifiers, names which distinguish them from all other variables. This is similar to elementary algebra, when one is taught to write ``Let a stand for the acceleration of the body ...''. Here a is an identifier for the value of the acceleration.
Rules for a valid identifer or a variable name
The rules of C and C plus plus for valid identifiers state that:
An identifier must:
start with a letter
consist only of letters, the digits 0-9, or the underscore symbol _
not be a reserved word
For the purposes of C/C++ identifiers, the underscore symbol( _ ) is considered to be a letter. Its use as the first character in an identifier is not recommended though, because many library functions in C++ use such identifiers. Similarly, the use of two consecutive underscore symbols, __, is forbidden.
The following are valid identifiers
length, days_in_year, DataSet1, Profit95
Int, _Pressure, first_one, first_1
The following are invalid:
days-in-year, 1data, int, first.val
Identifiers should be chosen to reflect the significance of the variable in the program being written. Although it may be easier to type a program consisting of single character identifiers but modifying or correcting the program becomes more and more difficult.
The minor typing effort of using meaningful identifiers can make the program very user-friendly.
At this stage it is worth noting that C++ is case-sensitive. That is lower-case letters are treated as distinct from upper-case letters. Thus the word main in a program is quite different from the word Main or the word MAIN.
Reserved words
The syntax rules (or grammar) of C++ define certain symbols to have a unique meaning within a C++ program. These symbols, are called Reserved Words and must not be used for any other purposes.The reserved word already used in first C program was void. All reserved words are in lower-case letters. The list below shows the reserved words of C/C++.
and and_eq asm auto bitand bitor bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new not not_eq operator or or_eq private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq
Some of these reserved words may not be treated as reserved by older compilers. Other compilers may add their own reserved words. Typical are those used by Borland compilers for the PC, which add near, far, huge, cdecl, and pascal.