By Stephen R. Davis

An integer variable declaration in C++ starts with the keyword int followed by the name of a variable and a semicolon, as in the following example:

int n1;       // declare a variable n1

All variables in C++ must be declared before they can be used. A variable declaration reserves a small amount of space in memory, just enough for a single integer (in this case), and assigns it a name. You can declare more than one variable in the same declaration, as in the following example, but it’s not a good idea:

int n2, n3;   // declare two variables n2 and n3

A keyword is a word that has meaning to C++. You cannot give a variable a name that’s the same as a keyword. Thus, for example, you can’t create a variable with the name int. However, keywords are case-sensitive, so you could create a variable Int or INT.

The fact that the keyword int is used instead of integer is just a reflection of the overall terseness of the C++ language. Makes you wonder whether the creators of the language were poor typists and wanted to minimize the amount of typing they had to do.

If you exceed the range of an int, you’ll get the wrong answer. Unlike in algebra class, the range of an integer is not unlimited in C++. However, it is very large indeed.

Variable names

You can name a variable anything you like — with the following restrictions:

  • The first letter of the variable must be a character in the sequence a through z, A through Z, or underscore (_).

  • Every letter after the first must be a character in the sequence a through z, A through Z, underscore (_), or the digits 0 through 9.

  • A variable name can be of any length. All characters are significant.

The following are legal variable names:

int myVariable;
int MyVariable;
int myNumber2Variable;
int _myVariable;
int my_Variable;

The following are not legal variable names:

int myPercentage%;      // contains illegal character
int 2ndVariable;        // starts with a digit
int my Variable;        // contains a space

Variable names should be descriptive. Variable names such as x are discouraged.

Assigning a value to a variable

Every variable has a value from the moment it’s declared. However, until you assign it a value, a variable will just assume whatever garbage value happens to be in that memory location when it’s allocated. So, if you don’t assign a value, you don’t know what value is lurking in that variable — and it’s likely to change every time you run the program.

You can assign a variable a value by using the equals sign, as in the following example:

int n;        // declare a variable n
n = 1;        // set it to 1

This looks remarkably similar to the assignment statement in algebra class, but the effect is not quite the same. In C++, the assignment statement says, “Take the value on the right-hand side of the equals sign (in this case, 1) and store it in the location on the left-hand side, overwriting whatever was there before (in this case, n).”

You can see the difference in the following expression:

n = n + 1;    // increment the variable n

This statement would make absolutely no sense in algebra class. How could n be both equal to n and n + 1 at the same time? However, this statement makes perfect sense in C++ if you follow the definition for assignment just given: “Take the value stored in the variable n (1) ,add 1, and store the result (2) in the variable n.” This is shown graphically here.

image0.jpg

Initializing a variable at declaration

You can initialize your variable at the time it’s declared by following it with an equals sign and a value:

int n = 1;    // declare and initialize variable

This statement initializes only the one variable, so if you write the following compound declaration

int n1, n2 = 0;

you’ve initialized n2 but not n1. This is one reason it’s not a good idea to declare multiple variables in a single declaration.