5.7 Types of Integers

I l @ ve RuBoard

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 [1] ). 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.

[1] Perl does have numbers such as 5, 8.3, and 20.8, but they are identical to the strings "5", "8.3", and "20.8".

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 (-2 15 ) 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 (2 16 - 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. [2]

[2] Borland-C++ even has a command-line switch to make the default for type char either signed or unsigned.

Question: Is the following character variable signed or unsigned?

 char foo; 

Answers:

  1. It's signed.

  2. It's unsigned.

  3. It's compiler-dependent.

  4. 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.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net