C++

CREATING MORE THAN ONE VARIABLE AT A TIME

You can create more than one variable of the same type in one statement by writing the type and then the variable names, separated by commas. For example:

unsigned int myAge, myWeight; // two unsigned int variables

long area, width, length; // three longs

As you can see, myAge and myWeight are each declared as unsigned integer variables. The second line declares three individual long variables named area, width, and length. The type (long) is assigned to all the variables, so you cannot mix types in one definition statement.

Assigning Values to Your Variables

You assign a value to a variable by using the assignment operator (=). Thus, you would assign 5 to Width by writing

unsigned short Width;

Width = 5;

You can combine these steps and initialize Width when you define it by writing

unsigned short Width = 5;

Initialization looks very much like assignment, and with integer variables, the difference is minor. Later, when constants are covered, you will see that some values must be initialized because they cannot be assigned to. The essential difference is that initialization takes place at the moment you create the variable.

Just as you can define more than one variable at a time, you can initialize more than one variable at creation. For example:

// create two long variables and initialize them

long width = 5, length = 7;

This example initializes the long integer variable width to the value 5 and the long integer variable length to the value 7. You can even mix definitions and initializations:

int myAge = 39, yourAge, hisAge = 40;

This example creates three type int variables, and it initializes the first and third.

Listing 3.2 shows a complete program, ready to compile, that computes the area of a rectangle and writes the answer to the screen.

Listing 3.2. A demonstration of the use of variables.

1: // Demonstration of variables

2: #include <iostream.h>

3:

4: int main()

5: {

6: unsigned short int Width = 5, Length;

7: Length = 10;

8:

9: // create an unsigned short and initialize with result

10: // of multiplying Width by Length

11: unsigned short int Area = Width * Length;

12:

13: cout << "Width:" << Width << "\n";

14: cout << "Length: " << Length << endl;

15: cout << "Area: " << Area << endl;

16: return 0;

17: }

Output:

Width:5

Length: 10

Area: 50

Analysis: Line 2 includes the required include statement for the iostream’s library so that cout will work. Line 4 begins the program.

On line 6, Width is defined as an unsigned short integer, and its value is initialized to 5. Another unsigned short integer, Length, is also defined, but it is not initialized. On line 7, the value 10 is assigned to Length.

On line 11, an unsigned short integer, Area, is defined, and it is initialized with the value obtained by multiplying Width times Length. On lines 13-15, the values of the variables are printed to the screen. Note that the special word endl creates a new line.

typedef

It can become tedious, repetitious, and, most important, error-prone to keep writing unsigned short int. C++ enables you to create an alias for this phrase by using the keyword typedef, which stands for type definition.

In effect, you are creating a synonym, and it is important to distinguish this from creating a new type (which you will do in Unit 6). typedef is used by writing the keyword typedef, followed by the existing type and then the new name. For example

typedef unsigned short int USHORT

creates the new name USHORT that you can use anywhere you might have written unsigned short int. Listing 3.3 is a replay of Listing 3.2, using the type definition USHORT rather than unsigned short int.

Listing 3.3. A demonstration of typedef.

1: // *****************

2: // Demonstrates typedef keyword

3: #include <iostream.h>

4:

5: typedef unsigned short int USHORT; //typedef defined

6:

7: void main()

8: {

9: USHORT Width = 5;

10: USHORT Length;

11: Length = 10;

12: USHORT Area = Width * Length;

13: cout << "Width:" << Width << "\n";

14: cout << "Length: " << Length << endl;

15: cout << "Area: " << Area <<endl;

16: }

Output: 

Width:5

Length: 10

Area: 50

Analysis: On line 5, USHORT is typedefined as a synonym for unsigned short int. The program is very much like Listing 3.2, and the output is the same.

When to Use short and When to Use long

One source of confusion for new C++ programmers is when to declare a variable to be type long and when to declare it to be type short. The rule, when understood, is fairly straightforward: If there is any chance that the value you’ll want to put into your variable will be too big for its type, use a larger type.

As seen in Table 3.1, unsigned short integers, assuming that they are two bytes, can hold a value only up to 65,535. Signed short integers can hold only half that. Although unsigned long integers can hold an extremely large number (4,294,967,295) that is still quite finite. If you need a larger number, you’ll have to go to float or double, and then you lose some precision. Floats and doubles can hold extremely large numbers, but only the first 7 or 19 digits are significant on most computers. That means that the number is rounded off after that many digits.

Wrapping Around an unsigned Integer

The fact that unsigned long integers have a limit to the values they can hold is only rarely a problem, but what happens if you do run out of room?

When an unsigned integer reaches its maximum value, it wraps around and starts over, much as a car odometer might. Listing 3.4 shows what happens if you try to put too large a value into a short integer.

Listing 3.4. A demonstration of putting too large a value in an unsigned integer.

1: #include <iostream.h>

2: int main()

3: {

4: unsigned short int smallNumber;

5: smallNumber = 65535;

6: cout << "small number:" << smallNumber << endl;

7: smallNumber++;

8: cout << "small number:" << smallNumber << endl;

9: smallNumber++;

10: cout << "small number:" << smallNumber << endl;

11: return 0;

12: }

Output: 

small number:65535

small number:0

small number:1

Analysis: On line 4, smallNumber is declared to be an unsigned short int, which on my computer is a two-byte variable, able to hold a value between 0 and 65,535. On line 5, the maximum value is assigned to smallNumber, and it is printed on line 6.

On line 7, smallNumber is incremented; that is, 1 is added to it. The symbol for incrementing is ++ (as in the name C++—an incremental increase from C). Thus, the value in smallNumber would be 65,536. However, unsigned short integers can’t hold a number larger than 65,535, so the value is wrapped around to 0, which is printed on line 8.

On line 9 smallNumber is incremented again, and then its new value, 1, is printed.

Back to Index