5.7 Types of Integers
C++ is considered a medium-level language because it allows you to get very close to the actual hardware of the machine. Some languages, such as Perl, go to great lengths to completely isolate the user from the details of how the processor works. This consistency comes at a great loss of efficiency. C++ lets you give detailed information about how the hardware is to be used.
For example, most machines let you use different-length numbers. Perl allows you to use only one simple data type (the string). This simplifies programming, but Perl programs are inefficient. C++ allows you to specify many different kinds of integers so you can make best use of the hardware.
The type specifier int tells C++ to use the most efficient size (for the machine you are using) for the integer. This can be 2 to 4 bytes depending on the machine. Sometimes you need extra digits to store numbers larger than what is allowed in a normal int.
The declaration:
long int answer; // the answer of our calculations
is used to allocate a long integer. The long qualifier informs C++ that you wish to allocate extra storage for the integer. If you are going to use small numbers and wish to reduce storage, use the qualifier
short.
short int year; // Year including the century
C++ guarantees that the storage for short <= int <= long. In actual practice, short almost always allocates 2 bytes; long, 4 bytes; and int, 2 or 4 bytes. (See Appendix B for numeric ranges.)
Long integer constants end with the character "L". For example:
long int var = 1234L; // Set up a long variable
Actually you can use either an uppercase or a lowercase "L". Uppercase is preferred since lowercase easily gets confused with the digit "1".
long int funny = 12l; // Is this 12<long> or one hundred twenty-one?
The type short int usually uses 2 bytes, or 16 bits. Normally, 15 bits are used for the number and 1 bit for the sign. This results in a range of -32,768 (-215) to 32,767 (215 - 1).
An unsigned short int uses all 16 bits for the number, giving it a range of 0 to 65,535 (216 - 1). All int declarations default to signed, so the declaration:
signed long int answer; // final result
is the same as:
long int answer; // final result
Finally there is the very short integer, the type char. Character variables are usually 1 byte long. They can also be used for numbers in the range of -128 to 127 or 0 to 255. Unlike integers, they do not default to signed; the default is compiler-dependent.
Question: Is the following character variable signed or unsigned?
char foo;
Answers:
It's signed.
It's unsigned.
It's compiler-dependent.
If you always specify signed or unsigned, you don't have to worry about problems like this.
Reading and writing very short integers is a little tricky. If you try to use a char variable in an output statement, it will be written�as a character. You need to trick C++ into believing that the char variable is an integer. This can be accomplished with the static_cast operator. Example 5-12 shows how to write a very short integer as a number.
Example 5-12. two2/twoc.cpp
#include <iostream>
signed char ch; // Very short integer // Range is -128 to 127
int main( ) { std::cout << "The number is " << static_cast<int>(ch) << '\n'; return (0); }
We start by declaring a character variable ch. This variable is assigned the value 37. This is actually an integer, not a character, but C++ doesn't care. On the next line, we write out the value of the variable. If we tried to write ch directly, C++ would treat it as a character. The code static_cast<int>(ch) tells C++, "Treat this character as an integer."
Reading a very short integer is not possible. You must first read in the number as a short int and then assign it to a very short integer variable.
5.7.1 Summary of Integer Types
long int
declarations allow the programmer to explicitly specify extra precision where it is needed (at the expense of memory). short int numbers save space but have a more limited range. The most compact integers have type char. They also have the most limited range.
unsigned numbers provide a way of doubling the range at the expense of eliminating negative numbers. The kind of number you use will depend on your program and storage requirements. The ranges of the various types of integers are listed in Appendix B.
|
No comments:
Post a Comment