Understanding Overflow and Underflow

Another source of potential conflict can arise when the value of a variable becomes too large or too small for its type for the computer running the application. When this occurs, it's called overflow and underflow. Depending on the computer, this will either cause an error or return alternative results.

This happens most frequently with floats, and it will obviously mess up any calculations made with such a value. When you attempt to print this variable, you'll most likely see either inf or infinity, indicating that the value is out of range.

The best way to avoid these problems is by knowing the limits for a particular type. This information is stored in the limits.h (for integers and characters) and float.h header files. The following example shows how to use these files to find the maximum and minimum value of a type. Just for fun, it also demonstrates what happens when overflow or underflow occurs.

To cause underflow and overflow

1.
Create a new C source file, beginning with the first include (Script 3.4):

/* let_it_flow.c - Script 3.4 */
#include <stdio.h>

Script 3.4. Using numbers outside the range of possible values results in overflow or underflow. In this application, variables are set to their maximum or minimum value, then increased or decreased.


2.
Include the two limit files:

#include <limits.h>
#include <float.h>

The first line will add the contents of limits.h to this application. The second will add the contents of float.h. Together, this makes available certain constants that will be used in this application.

3.
Start defining the main() function:

int main(void) {

4.
Declare and initialize the variables to be used:

short int short_max = SHRT_MAX;
int integer_min = INT_MIN;
double double_max = DBL_MAX;

Using the constants defined in the two included files, three variables are initialized to maximum and minimum values for different types. SHRT_MAX represents the largest possible value of a short integer; INT_MIN represents the smallest possible value for an integer; and DBL_MAX represents the largest possible value for a double.

5.
Cause each variable to over-or underflow:

short_max++;
integer_min *= 2;
double_max *= 10; 

Since short_max already has the value of the maximum possible short integer, increasing it by one will cause that variable to overflow. Conversely, doubling the value of integer_min (the smallest possible integer), will create an even smaller integer, as doubling a negative number makes it twice as small. Finally, the double is multiplied by 10 to create a larger double than the largest possible value.

6.
Print out the results:

printf ("The maximum short integer is
 %d. Overflowed, it looks like
 %d.
", SHRT_MAX, short_max);

printf ("The minimum integer is %d.
 Underflowed, it looks like %d.
",
 INT_MIN, integer_min);

printf ("The maximum double is %f.
 Overflowed, it looks like %f.
",
DBL_MAX, double_max);

These three print statements will each print the original constant value (the maximum or minimum), followed by the manipulated variable. This way, you'll be able to see the limits—which correspond to the original variable value—and the result of overflow and underflow.

7.
Complete the function definition:

    getchar();
    return 0;
}

8.
Save the file as let_it_flow.c.

9.
Compile and debug (as necessary) the application.

10.
Run the compiled application (Figures 3.7 and 3.8).

Figure 3.7. Running the application using Xcode on Mac OS X. Notice how each variable type responds differently in terms of the end value.


Figure 3.8. The results using Dev-C++ on Windows. The overflowed double is displayed slightly differently (compare with Figure 3.7).


✓ Tips

  • Both limits.h and float.h are standard C header files, just like stdio.h, meaning they should be part of the C distribution that comes with your IDE. You'll learn more about header files and the syntax for including them in Chapter 8, “Using the C Preprocessor.”

  • You can find all the limits.h and float.h constants by searching the Internet or by viewing those files on your computer.

  • If you see NaN displayed, this means “Not a Number,” indicating that the variable either has a non-number value due to over- or underflow.


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

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