2.2.6 A Simple Example for Integer Overflows
Integer overflow is a critical concept in C programming, especially because C does not perform automatic overflow detection. Understanding how and why it happens requires a look into binary representation, two’s complement arithmetic, integer limits, and the behavior of C compilers.
1. How Integers are Represented in Memory
Computers store numbers in binary format using a fixed number of bits. The way these bits are interpreted depends on whether the number is signed (can be positive or negative) or unsigned (only positive).
1.1 Unsigned Integers
Unsigned integers can only store non-negative values. In a 4-bit system (for simplicity), the numbers are represented as follows:
Decimal | Binary |
---|---|
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
8 | 1000 |
9 | 1001 |
10 | 1010 |
11 | 1011 |
12 | 1100 |
13 | 1101 |
14 | 1110 |
15 | 1111 |
The highest value is
1111
(15 in decimal).If we add 1 to
1111
, we get10000
, but since this is a 4-bit system, the leftmost bit is discarded, causing the value to wrap around to0000
(0 in decimal).This is an example of unsigned integer overflow.
1.2 Signed Integers and Two’s Complement
Signed integers use the two’s complement representation, which allows for both positive and negative numbers.
In a 4-bit signed integer system, the representation is:
Decimal | Binary |
---|---|
-8 | 1000 |
-7 | 1001 |
-6 | 1010 |
-5 | 1011 |
-4 | 1100 |
-3 | 1101 |
-2 | 1110 |
-1 | 1111 |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
The leftmost bit is called the sign bit:
0
= positive1
= negative
The negative numbers are stored in two’s complement form, which is obtained by flipping all bits and adding 1.
Example: Two’s Complement of -5
Write
5
in binary:0101
Flip all bits:
1010
Add 1:
1011
(which is -5 in two’s complement)
1.3 Overflow in Signed Integers
Let’s consider a 4-bit signed integer:
The highest positive value is
0111
(7
in decimal).If we add 1 to
7
, we get1000
, which is-8
in two’s complement.This is an example of signed integer overflow, where adding to the max value wraps around to the min value.
For a 32-bit signed integer, the maximum value is:
0111 1111 1111 1111 1111 1111 1111 1111 = 2,147,483,647
Adding 1
causes overflow:
1000 0000 0000 0000 0000 0000 0000 0000 = -2,147,483,648
2. Integer Limits in C
C defines the minimum and maximum values for integer types in the limits.h
library:
Type | Min Value | Max Value |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
When an integer exceeds these limits, overflow occurs.
3. How Overflow Happens in C
3.1 Example: Signed Integer Overflow
#include <stdio.h>
#include <limits.h>
int main() {
int max = INT_MAX; // 2147483647
printf("Max int: %d\n", max);
max = max + 1; // Overflow occurs
printf("After overflow: %d\n", max);
return 0;
}
Expected Output (32-bit system)
Max int: 2147483647
After overflow: -2147483648
Why?
INT_MAX = 2,147,483,647
Adding
1
results in-2,147,483,648
due to two’s complement wrap-around.
3.2 Example: Unsigned Integer Overflow
#include <stdio.h>
#include <limits.h>
int main() {
unsigned int max = UINT_MAX; // 4294967295
printf("Max unsigned int: %u\n", max);
max = max + 1; // Overflow occurs
printf("After overflow: %u\n", max);
return 0;
}
Expected Output (32-bit system)
Max unsigned int: 4294967295
After overflow: 0
Why?
UINT_MAX = 4,294,967,295
Adding
1
results in0
due to wrap-around.
4. Detecting and Preventing Overflow
4.1 Using limits.h
for Safe Addition
#include <stdio.h>
#include <limits.h>
int main() {
int x = INT_MAX;
if (x > INT_MAX - 1) { // Prevent overflow
printf("Overflow detected!\n");
} else {
x = x + 1;
printf("New value: %d\n", x);
}
return 0;
}
This checks before adding to prevent overflow.
4.2 Using Wider Data Types
If int
is too small, you can use long long int
:
long long int large = 2147483647LL + 1LL; // No overflow
printf("%lld\n", large);
5. Compiler Warnings & Undefined Behavior
C does not always detect overflow. However, compilers can warn about it:
gcc -Wall -Wextra -o overflow overflow.c
This may show warnings about integer overflow.
Conclusion
Integer overflow happens when a number exceeds its max representable value.
Signed integers use two’s complement, leading to negative wrap-around.
Unsigned integers wrap around to zero.
Use
limits.h
to check limits and avoid overflow.Use wider data types (
long long
) if necessary.