I l@ve RuBoard Previous Section Next Section

4.4 Variables and Storage

C++ allows you to store values in variables. Each variable is identified by a variable name.

Additionally, each variable has a variable type. The type tells C++ how the variable is going to be used and what kind of numbers (real, integer) it can hold.

Names start with a letter followed by any number of letters, digits, or underscores ( _ ).[3] Uppercase is different from lowercase, so the names "sam", "Sam", and "SAM" specify three different variables. To avoid confusion, it is better to use different names for variables and not depend on case differences.

[3] System variable names (names defined the by the standard library) can begin with underscore.

Some C++ programmers use all lowercase variable names. Some names, such as int, while, for, and float, have a special meaning to C++ and are considered reserved words , also called keywords. They cannot be used for variable names.

The following is an example of some variable names:

average            // average of all grades
pi                 // pi to 6 decimal places
number_of_students // number of students in this class

The following are not variable names:

3rd_entry   // Begins with a number
all$done    // Contains a "$"
the end     // Contains a space
int         // Reserved word

Avoid variable names that are similar. For example, the following illustrates a poor choice of variable names:

total       // total number of items in current entry
totals      // total of all entries

This is a much better set of names:

entry_total // total number of items in current entry
all_total   // total of all entries
    I l@ve RuBoard Previous Section Next Section