A byte usually means an 8-bit unit of memory. Byte in this sense is the unit of measurement that describes the amount of memory in a computer, with a kilobyte equal to 1,024 bytes and a megabyte equal to 1,024 kilobytes. However, C++ defines byte differently. The C++ byte consists of at least enough adjacent bits to accommodate the basic character set for the implementation. That is, the number of possible values must equal or exceed the number of distinct characters. In the United States, the basic character sets are usually the ASCII and EBCDIC sets, each of which can be accommodated by 8 bits, so the C++ byte is typically 8 bits on systems using those character sets. However, international programming can require much larger character sets, such as Unicode, so some implementations may use a 16-bit byte or even a 32-bit byte. Some use the term octet to denote an 8-bit byte.

Many systems currently use the minimum guarantee, making short 16 bits and long 32 bits. This still leaves several choices open for int. It could be 16, 24, or 32 bits in width and meet the standard. It could even be 64 bits, providing that long and long long are at least that wide. Typically, int is 16 bits (the same as short) for older IBM PC implementations and 32 bits (the same as long) for Windows XP, Windows Vista, Windows 7, Macintosh OS X, VAX, and many other minicomputer implementations. Some implementations give you a choice of how to handle int. (What does your implementation use? The next example shows you how to determine the limits for your system without your having to open a manual.) The differences between implementations for type widths can cause problems when you move a C++ program from one environment to another, including using a different compiler on the same system. But a little care, as discussed later in this chapter, can minimize those problems.

You use these type names to declare variables just as you would use int:

short score;             // creates a type short integer variable

int temperature;         // creates a type int integer variable

long position;           // creates a type long integer variable

Actually, short is short for short int and long is short for long int, but hardly anyone uses the longer forms.

The four types—int, short, long, and long long—are signed types, meaning each splits its range approximately equally between positive and negative values. For example, a 16-bit int might run from –32,768 to +32,767.

If you want to know how your system’s integers size up, you can use C++ tools to investigate type sizes with a program. First, the sizeof operator returns the size, in bytes, of a type or a variable. (An operator is a built-in language element that operates on one or more items to produce a value. For example, the addition operator, represented by +, adds two values.) Recall that the meaning of byte is implementation dependent, so a 2-byte int could be 16 bits on one system and 32 bits on another. Second, the climits header file (or, for older implementations, the limits.h header file) contains information about integer type limits. In particular, it defines symbolic names to represent different limits. For example, it defines INT_MAX as the largest possible int value and CHAR_BIT as the number of bits in a byte. Listing 3.1 demonstrates how to use these facilities. The program also illustrates initialization, which is the use of a declaration statement to assign a value to a variable.

Listing 3.1. limits.cpp

// limits.cpp -- some integer limits

#include

#include               // use limits.h for older systems

int main()

{

    using namespace std;

    int n_int = INT_MAX;        // initialize n_int to max int value

    short n_short = SHRT_MAX;   // symbols defined in climits file

    long n_long = LONG_MAX;

    long long n_llong = LLONG_MAX;

    // sizeof operator yields size of type or of variable

    cout << "int is " << sizeof (int) << " bytes." << endl;

    cout << "short is " << sizeof n_short << " bytes." << endl;

    cout << "long is " << sizeof n_long << " bytes." << endl;

    cout << "long long is " << sizeof n_llong << " bytes." << endl;

    cout << endl;

    cout << "Maximum values:" << endl;

    cout << "int: " << n_int << endl;

    cout << "short: " << n_short << endl;

    cout << "long: " << n_long << endl;

    cout << "long long: " << n_llong << endl << endl;

    cout << "Minimum int value = " << INT_MIN << endl;

    cout << "Bits per byte = " << CHAR_BIT << endl;

    return 0;

}

Note

If your system doesn’t support the long_long type, you should remove the lines using that type.

Here is sample output from the program in Listing 3.1:

int is 4 bytes.

short is 2 bytes.

long is 4 bytes.

long long is 8 bytes.

Maximum values:

int: 2147483647

short: 32767

long: 2147483647

long long: 9223372036854775807

Minimum int value = -2147483648

Bits per byte = 8

These particular values came from a system running 64-bit Windows 7.

The following sections look at the chief programming features for this program.

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги