- What Is a Variable?
- Defining a Variable
- Creating More Than One Variable at a Time
- Assigning Values to Your Variables
- typedef
- When to Use short and When to Use long
- Characters
- Constants
- Enumerated Constants
- Summary
- Q&A
- Workshop
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 any chance exists that the value you'll want to put into your variable will be too big for its type, use a larger type.
As shown 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 split their values between positive and negative numbers, and thus their maximum value is only half that of the unsigned.
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 9 digits are significant on most computers. That means that the number is rounded off after that many digits.
Shorter variables use up less memory. These days, memory is cheap and life is short. Feel free to use int, which will probably be four bytes on your machine.
Wrapping Around an unsigned Integer
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
0: #include <iostream> 1: int main() 2: { 3: 4: using std::cout; 5: using std::endl; 6: 7: unsigned short int smallNumber; 8: smallNumber = 65535; 9: cout << "small number:" << smallNumber << endl; 10: smallNumber++; 11: cout << "small number:" << smallNumber << endl; 12: smallNumber++; 13: cout << "small number:" << smallNumber << endl; 14: return 0; 15: }
Output
small number:65535 small number:0 small number:1
Analysis On line 7, 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 8, the maximum value is assigned to smallNumber, and it is printed on line 9.
On line 10, 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 11.
On line 12 smallNumber is incremented again, and then its new value, 1, is printed.
Wrapping Around a signed Integer
A signed integer is different from an unsigned integer, in that half of the values you can represent are negative. Instead of picturing a traditional car odometer, you might picture a clock much like the one shown in Figure 3.2, in which the numbers count upward moving clockwise and downward moving counter-clockwise. They cross at the bottom of the clock face (traditional 6 o'clock).
Figure 3.2 If clocks used signed numbers.
One number from 0 is either 1 (clockwise) or -1 (counter-clockwise). When you run out of positive numbers, you run right into the largest negative numbers and then count back down to 0. Listing 3.5 shows what happens when you add 1 to the maximum positive number in short integer.
Listing 3.5 A Demonstration of Adding Too Large a Number to a signed Integer
0: #include <iostream> 1: int main() 2: { 3: short int smallNumber; 4: smallNumber = 32767; 5: std::cout << "small number:" << smallNumber << std::endl; 6: smallNumber++; 7: std::cout << "small number:" << smallNumber << std::endl; 8: smallNumber++; 9: std::cout << "small number:" << smallNumber << std::endl; 10: return 0; 11: }
Output
small number:32767 small number:-32768 small number:-32767
Analysis On line 4, smallNumber is declared this time to be a signed short integer (if you don't explicitly say that it is unsigned, it is assumed to be signed). The program proceeds much as the preceding one, but the output is quite different. To fully understand this output, you must be comfortable with how signed numbers are represented as bits in a two-byte integer.
The bottom line, however, is that just like an unsigned integer, the signed integer wraps around from its highest positive value to its highest negative value.