4.6 Integers
One variable type
is integer. Integers (also known as whole numbers) have no fractional
part or decimal point. Numbers such as 1, 87, and -222 are integers.
The number 8.3 is not an integer because it contains a decimal point.
The general form of an integer declaration is:
int name; // comment
A calculator with an eight-digit display can only handle numbers
between 99,999,999 and -99,999,999. If you try to add 1 to
99,999,999, you will get an overflow error. Computers have similar
limits. The limits on integers are implementation- dependent, meaning
they change from computer to computer.
Calculators use
decimal digits (0-9). Computers use binary digits (0-1) called
bits. Eight bits make a
byte. The number of bits used to hold an integer
varies from machine to machine. Numbers are converted from binary to
decimal for printing.
On most machines integers are 32 bits (4 bytes), providing a range of
2,147,483,647 (231- 1) to -2,147,483,648
(-231). Some systems now use newer
processors such as the Intel Itanium, which have 64-bit integers,
giving you a range of 9223372036854775807
(263-1) to -9223372036854775807
(-263) If you are programming using an
older MS-DOS compiler, only 16 bits (2 bytes) are used, so the range
is 32,767 (215-1) to -32,768
(-215).
Question 4-1: The following will work on a Unix machine but will fail on an old
MS-DOS system.
int zip; // zip code for current address
.........
zip = 92126;
Why does this fail? What will be the result when this program is run
on an MS-DOS system?
|