Previous Table of Contents Next


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.

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 Demonstrates 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
	   ⇒// of multiplying Width by Length
10:     unsigned short int Area  = Width * Length;
11:
12:     cout << “Width:” << Width << “\n”;
13:     cout << “Length: “  << Length << endl;
14:     cout << “Area: “ << Area << endl;
15:     return 0;
16:  }

Width: 5
Length: 10
Area: 50
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 integer, and its value is initialized to 5. Another integer, Length, is also defined, but it’s not initialized. On line 7 the value 10 is assigned to Length.

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

endl stands for end line and is end-ell rather than end-one. It is commonly pronounced “end-ell.”

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 on Day 6, “Basic Classes”). 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 Demonstrates typedef


1:   // *****************
2:   // Demonstrates typedef keyword
3:   #include <iostream.h>
4:
5:   typedef unsigned short int USHORT;       //typedef defined
6:
7:   int 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:     return 0;
17:  }

Width: 5
Length: 10
Area: 50
On some compilers, this code will issue a warning that the “conversion may lose significant digits.” This is because the product of the two USHORTS on line 12 might be larger than an unsigned short can hold, and assigning that product to Area can cause truncation. In this particular case, you can safely ignore this warning.

On line 5, USHORT is typedef’d as a synonym for unsigned short int. The program is otherwise identical to 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.


Previous Table of Contents Next