Next: Reserved words Up: A simple C++ program Previous: A simple C++ program

Variables

A variable is the name used for the quantities which are manipulated by a computer program. For example a program that reads a series of numbers and sums them will have to have a variable to represent each number as it is entered and a variable to represent the sum of the numbers.

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. The rules of C++ for valid identifiers state that:

An identifier must:
Reserved words are otherwise valid identifiers that have special significance to C++. A full list is given below in section 6.1.1. For the purposes of 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
although using _Pressure is not recommended.
The following are invalid:
days-in-year 1data int first.val throw

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, modifying or correcting the program becomes more and more difficult. The minor typing effort of using meaningful identifiers will repay itself many fold in the avoidance of simple programming errors when the program is modified.

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.



Subsections

Next: Reserved words Up: A simple C++ program Previous: A simple C++ program