![](https://csdnimg.cn/release/download_crawler_static/1895227/bg14.jpg)
An Overview of C++ – 12 –
Integer types
Although the concept of an integer seems like a simple one, C++ actually includes several
different data types for representing integer values. In most cases, all you need to know
is the type
int, which corresponds to the standard representation of an integer on the
computer system you are using. In certain cases, however, you need to be more careful.
Like all data, values of type int are stored internally in storage units that have a limited
capacity. Those values therefore have a maximum size, which limits the range of
integers you can use. To get around this problem, C++ defines three integer types—
short, int, and long—distinguished from each other by the size of their domains.
Unfortunately, the language definition for C++ does not specify an exact range for
these three types. As a result, the range for the different integer types depends on the
machine and the compiler you’re using. On many personal computers, the maximum
value of type
int is 32,767, which is rather small by computational standards. If you
wanted, for example, to perform a calculation involving the number of seconds in a year,
you could not use type int on those machines, because that value (31,536,000) is
considerably larger than the largest available value of type int. The only properties you
can rely on are the following:
• The internal size of an integer cannot decrease as you move from short to int to
long. A compiler designer for C++ could, for example, decide to make short and int
the same size but could not make int smaller than short.
• The maximum value of type
int must be at least 32,767 (2
15
–1).
• The maximum value of type
long must be at least 2,147,483,647 (2
31
–1).
The designers of C++ could have chosen to define the allowable range of type int
more precisely. For example, they could have declared—as the designers of Java did—
that the maximum value of type int would be 2
31
–1 on every machine. Had they done
so, it would be easier to move a program from one system to another and have it behave
in the same way. The ability to move a program between different machines is called
portability, which is an important consideration in the design of a programming
language.
In C++, each of the integer types
int, long, and short may be preceded by the
keyword unsigned. Adding unsigned creates a new data type in which only
nonnegative values are allowed. Because unsigned variables do not need to represent
negative values, declaring a variable to be one of the unsigned types allows it to hold
twice as many positive values. For example, if the maximum value of type int is 32,767,
the maximum value of type unsigned int will be 65,535. C++ allows the type unsigned
int to be abbreviated to unsigned, and most programmers who use this type tend to
follow this practice. Sometimes variables intended to store sizes are declared as
unsigned, because a size will always be nonnegative.
An integer constant is ordinarily written as a string of digits representing a number in
base 10. If the number begins with the digit 0, however, the compiler interprets the value
as an octal (base 8) integer. Thus, the constant
040 is taken to be in octal and represents
the decimal number 32. If you prefix a numeric constant with the characters 0x, the
compiler interprets that number as hexadecimal (base 16). Thus, the constant 0xFF is
equivalent to the decimal constant 255. You can explicitly indicate that an integer
constant is of type long by adding the letter L at the end of the digit string. Thus, the
constant 0L is equal to 0, but the value is explicitly of type long. Similarly, if you use the
letter U as a suffix, the constant is taken to be unsigned.