- 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
- Constants
- Enumerated Constants
- 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 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 in unsigned Integers
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 -toobigu.cpp Demonstrates Putting Too Large a Value in an unsigned Integer
0: #include <iostream> 1: 2: int main() 3: { 4: unsigned short int smallNumber; 5: smallNumber = 65535; 6: std::cout << "small number:" << smallNumber << std::endl; 7: smallNumber++; 8: std::cout << "small number:" << smallNumber << std::endl; 9: smallNumber++; 10: std::cout << "small number:" << smallNumber << std::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. But 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 its new value, 1, is printed.
Wrapping Around a signed Integer
A signed integer is different from an unsigned integer in that half of its values are negative. Instead of picturing a traditional car odometer, you might picture one that rotates up for positive numbers and down for negative numbers. One mile from zero is either 1 or 1. When you run out of positive numbers, you run right into the largest negative numbers and then count back down to zero. Listing 3.5 shows what happens when you add 1 to the maximum positive number in an unsigned short integer.
Listing 3.5 -toobigs.cpp Demonstrates Adding Too Large a Number to a signed Integer
0: #include <iostream> 1: 2: int main() 3: { 4: short int smallNumber; 5: smallNumber = 32767; 6: std::cout << "small number:" << smallNumber << std::endl; 7: smallNumber++; 8: std::cout << "small number:" << smallNumber << std::endl; 9: smallNumber++; 10: std::cout << "small number:" << smallNumber << std::endl; 11: return 0; 12: }
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. If we were to decrement from 32,767, we would get 32,768, and then 32,767, 32,766, and so on.