Selecting a Number Type

Choosing the right type of number to use in a C application is critical, as it affects both the range of computational possibilities and the overall performance. Each of the available number types has its own strengths and weaknesses. More important, each has its own dangers, which can trip up even a thoughtful programmer.

C supports two broad types of numbers: integers (1, 2345, -8) and floating points (3.14, -0.8, 23.2). The latter always contain decimal points, whereas the former never do. Each of these is then further broken down into subtypes based on their size. Floats can also be represented as doubles, which are twice as accurate (more on this later). Also, integers (but not floats and doubles) can be defined as signed—meaning they can be positive or negative—or unsigned (in which case they cannot be negative).

The possible range of values for a specific number type depends on the operating system of the computer and how much memory is assigned to that type (Table 3.1). As a rule, int (the standard integer type) will always be the most efficient size (in terms of memory usage) for the computer on which the application is compiled.

Table 3.1. This table represents the general range of values and memory requirements of the different number types for the average computer.
Number Types and Approximate Sizes
TypeMemory UsageRange of Values
short int2 bytes-32,768 to 32,767
unsigned short int2 bytes0 to 65,535
int4 bytes-2,147,483,648 to 2,147,483,647
unsigned int4 bytes0 to 4,294,967,295
long int4 bytes-2,147,483,648 to 2,147,483,647
unsigned long int4 bytes0 to 4,294,967,295
long long int8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long long int8 bytes0 to 18,446,744,073,709,551,615
float4 bytes-1e38 to +1e38
double8 bytes-1e308 to +1e308
long double8 bytes-1e308 to +1e308

Conversely, short ints tend to be only 2 bytes and long ints are 4. The actual memory usage will differ depending on the computer; the only thing you can assume is that a short int is smaller than or equal to an int and that an int is smaller than or equal to a long int. This also means that if a compiler gives the same amount of memory to a short int and an int, they'll both require 2 bytes of memory and have a range of -32,768 to 32,767. If the compiler gives the same amount of memory to both int and long int, they'll both require 4 bytes of memory and have a range of -2,147,483,648 to 2,147,483,647.

Floats normally require 4 bytes of memory and doubles 8. Computers are theoretically slower in dealing with floats (and more so for doubles), although most modern computers use a floating-point processor, which greatly minimizes the performance difference.

C and Memory

One of the most complex aspects of programming in C is that you must judiciously manage memory usage (conversely, higher-level and scripting languages perform memory management for you). We deal with more complex memory management later in the book, but you should know now that selecting the right variable type will affect the amount of memory required by your applications.

Table 3.1 indicates the memory requirements of the different number types. For your reference, a bit is the smallest unit of memory, holding a single 0 or 1. A byte is equal to 8 bits. Therefore, a single byte can store up to 256 different combinations of zeroes and ones (either 0 or 1 in each of the eight slots). Your computer's memory is listed in megabytes, each of which is 1,024 kilobytes, where a kilobyte is 1,024 bytes. This may infer your C applications can go hog wild in memory usage, but a lot of memory is used by the operating system, so you shouldn't be writing applications that require 256 MB of RAM.

Finally, a word is the basic unit of memory for a particular computer. Hence, an old PC running MS-DOS has a word of 16 bits, or 2 bytes; most modern computers have a word of 32 bits, or 4 bytes; and something like Apple's new G5—which has a 64-bit processor—has a word of 64 bits, or 8 bytes.

All of this information is relevant because how much memory a type takes up—and therefore that type's possible range of values—depends on the computer's word. For starters, an int will normally require one word of memory. Most of today's computers, using 32-bit and x86 processors, have words of 4 bytes.


To select a number type

1.
Decide whether you will be working with a fraction.

If you know that the number being manipulated will have a decimal, it must be a floating point type (float, double, long double). If it won't require a decimal, then you're working within the integer group.

2.
If you will be using fractions, determine how important accuracy is.

As stated before, doubles are twice as accurate as floats. If you'll be working with numbers whose accuracy is paramount, go with a double. If fair approximations are acceptable, stick to the smaller float. In the next few examples you'll get a better sense of each type's level of accuracy.

3.
Consider what the maximum possible value for a number will be.

For example, if you wanted to store a U.S. zip code as an integer, you couldn't use a short int, as the maximum zip code value (somewhere in the 90000 realm) would be larger than the maximum short int size (32,767). When making this decision, err on the side of selecting a type too large over one that might be too small, particularly as you are just learning C.

4.
Finally, determine if the number can ever be negative, assuming it's an integer type.

If the value will never be negative (like a zip code, a person's age, or an IQ), opt for an unsigned type, thereby doubling the maximum range.

5.
Consider how the number will be used.

Specific actions, like division and finding the remainder of division, have their own type requirements, as you'll see throughout the rest of this chapter. You'll need to account for this idea when choosing a type as well.

✓ Tips

  • Exponential numbers are represented by the floating point type, as in Table 3.1.

  • Critical financial calculations are best performed using integers that don't have the inexact arithmetic that floats can. This can be accomplished by working in cents (175 cents) instead of dollars (1.75).

  • The long long int type is new as of the C99 standard.

  • Unless you specify otherwise, all forms of integers are signed by default.

  • The number ranges indicated in Table 3.1 may vary plus or minus one on different computers.

  • On a more advanced level, you can use other number types if you include the stdint.h header file.

  • The sizeof() function returns the size, in bytes, taken up by a specific variable or variable type. You can place a variable type within parentheses or precede a variable's name with just sizeof:

    short int temp = 3940;
    printf ("Temp: %zd; Int: %zd, Float:
     %zd
    ", sizeof (temp), sizeof(int),
     sizeof(float));
    

    Notice that you use the special %zd signifier to print the value, which is of a special type (sizeof() returns a value of type size_t).


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset