| Previous | Table of Contents | Next |
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, youll 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.
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 Demonstrates 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: }
![]() | small number: 65535 small number: 0 small number: 1 |
![]() | 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 cant 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.
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 short integer.
Listing 3.5 Demonstrates Adding Too Large a Number to a signed Integer
1: #include <iostream.h>
2: int main()
3: {
4: short int smallNumber;
5: smallNumber = 32767;
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: }
![]() | small number: 32767 small number: -32768 small number: -32767 |
![]() | On line 4, smallNumber is declared this time to be a signed short integer. (If you dont 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. For details, check Appendix C. |
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.
![]() | Like variables, constants are data storage locations. But variables vary; constants, on the other hand and as you might have guessed, do not vary. |
You must initialize a constant when you create it, and you cannot assign a new value later; once a constant is initialized its value is, in a word, constant.
![]() | C++ has two types of constants: literal and symbolic. |
A literal constant is a value typed directly into your program wherever it is needed. For example:
int myAge = 39;
myAge is a variable, of type int; 39 is a literal constant. You cant assign a value to 39, and its value cant be changed.
A symbolic constant is a constant that is represented by a name, just as a variable is. Unlike a variable, however, after a constant is initialized, its value cant be changed.
If your program has one integer variable named students and another named classes, you could compute how many students you have, given a known number of classes, if you knew there were 15 students per class:
students = classes * 15;
![]() |
|
In this example, 15 is a literal constant. Your code would be easier to read and easier to maintain, if you substituted a symbolic constant for this value:
students = classes * studentsPerClass
If you later decided to change the number of students in each class, you could do so where you define the constant studentsPerClass without having to make a change every place you used that value.
| Previous | Table of Contents | Next |