How to Avoid Integer Overflows and Underflows in C++?
Last Updated :
06 Apr, 2023
Integers in C++ are allocated with a certain number of bits. If an integer value, takes more bits than the allocated number of bits, then we may encounter an overflow or underflow.
- The integer overflow occurs when a number is greater than the maximum value the data type can hold.
- The integer underflow occurs when a number is smaller than the minimum value the data type can hold.
We deal mainly with these data types to store integers in C++. These are:
- signed int: The signed int data type ranges between -2,147,483,648 to 2,147,483,647 (-109 to 109).
- unsigned int: The unsigned int data type ranges between 0 to 4,294,967,295.
- long long: The long long data type ranges between -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (-1018 to 1018).
Let's discuss integer overflow and integer underflow in detail.
Integer Overflow
Example: In the below C++ program, three variables a, b and c are initialized as int(signed) data type:
C++
// C++ program to demonstrate
// integer overflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 100000;
int b = 100000;
int c = a * b;
cout << "The product of a and b is " <<
c << endl;
return 0;
}
OutputThe product of a and b is 1410065408
Time Complexity : O(1)
Auxiliary Space : O(1)
Explanation: The expected value of c is 1010 but the output is 1410065408. This is because int c can store a maximum range of 109.
Solution 1:
1. Initialize variable c as long long data type.
long long c = a * b;
2. But the problem still arises because a and b are int data types and the product of two int data types is always an integer ranges between the range of int which is mentioned above.
3. Initialize a or b as long long data types. Since multiplication of int and long long is long long. So, a and b will result in a long long range.
Below is the C++ program to implement the above solution to handle integer overflow:
C++
// C++ program to handle integer
// overflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 100000;
// Changed int b to long long b
long long b = 100000;
long long c = a * b;
cout << "The product of a and b is " <<
c << endl;
return 0;
}
OutputThe product of a and b is 10000000000
Solution 2:
1. Initialize variable c as long long data type.
long long c = a * b;
2. Instead of changing the data types of a and b, we can multiply a and b with 1LL while initializing the value of c so that multiplication of a and b with long long 1 also results into long long and that value will be stored in long long c.
Below is the C++ program to implement the above solution to handle integer overflow:
C++
// C++ program to handle integer
// overflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
int a = 100000;
int b = 100000;
// Here we multiplied 'a' with 1LL
// which results into long long
// which is further multiplied with 'b'
long long c = a * 1LL * b;
cout << "The product of a and b is " <<
c << endl;
return 0;
}
OutputThe product of a and b is 10000000000
Solution 3: Initialize all three variables a, b and c as long long data types initially. It will give our desired output but it will take some extra space.
Below is the C++ program to implement the above solution to handle integer overflow:
C++
// C++ program to handle integer
// overflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
// Here we changed the data types
// of both a and b to long long
// to avoid overflow
long long a = 100000, b = 100000;
long long c = a * b;
cout << "The product of a and b is " <<
c << endl;
return 0;
}
OutputThe product of a and b is 10000000000
Integer Underflow
Example 1: In the below code, 3 variables a, b and c are initialized then as unsigned int to show integer underflow:
C++
// C++ program to show integer
// underflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
unsigned int a = 4, b = 5;
unsigned int c = a - b;
cout << c;
return 0;
}
Explanation:
The expected value of c is -1 but the output is 4294967295. This is because unsigned int c cannot store a negative value.
Solution 1:
To fix the above problem initialize c as int(signed) to store a negative number. Below is the C++ program to show how to handle integer underflow:
C++
// C++ program to show how to
// handle integer underflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
unsigned int a = 4, b = 5;
// Here we changed data type of
// c to signed int
int c = a - b;
cout << c;
return 0;
}
Example 2: In the below code, variable a is initialized as unsigned int, b, and c are initialized as int to show integer underflow:
C++
// C++ program to show integer
// underflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
unsigned int a = 1000000;
int b = -10000;
int c = b * a;
cout << c;
return 0;
}
Explanation:
The expected value of c is -1010 but the output is -1410065408. This is because, int(signed) c cannot store a negative value smaller than -2,147,483,648.
Solution 1:
1. Initialize variable c as long long data type to store -1010.
long long c = b * a;
2. But the problem still arises as you can see below because a is a unsigned int while b is a signed int and the product of both of them cannot be a number in the range of long long, so we need to change one of them to a long long data type.
Below is the C++ program to handle integer underflow:
C++
// C++ program to handle
// integer underflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
unsigned int a = 1000000;
long long b = -10000;
long long c = b * a;
cout << c;
return 0;
}
Solution 2:
Instead of changing data types of a and b, we can multiply a and b with 1LL while initializing the value of c so that multiplication of a and b with long long 1 also results into long long and that value will be stored in long long c.
C++
// C++ program to handle
// integer underflow
#include <iostream>
using namespace std;
// Driver code
int main()
{
unsigned int a = 1000000;
int b = -10000;
long long c = b * 1LL * a;
cout << c;
return 0;
}
Similar Reads
How Do I Detect Unsigned Integer Overflow in C++? In C++, unsigned integer is a datatype that can store only zero and non-negative integer values. According to C++ standard, unsigned integer overflow is defined behavior (as it wraps around using modulo arithmetic and starts again from 0). So, to detect when this wrapping occurred is important to de
3 min read
How to Assign Negative Infinity in C++? In C++, the infinity is written as inf. We get infinity as a result when a positive number is divided by a null value or when a value is much greater and cannot be stored in 64-bit. In C++, positive infinity is defined in many libraries but the negative infinity is not defined. In this article, we w
2 min read
How to Overload the Less-Than (<) Operator in C++? In C++ we have an operator called less than operator (<) which checks if the left side operand is smaller than the right side operand or not. In this article, we will learn how to overload the less-than operator in C++. Overloading Less-Than Operator in C++In C++, we can overload the less-than op
2 min read
How to Handle Large Numbers in C++? In C++, when working with very large numbers the standard data types like int can become insufficient due to their limited range. In this article, we will learn how we can handle large numbers in C++. Handle Large Numbers in C++To store integers of different sizes, C++ offers built-in data types lik
3 min read
How to Handle Wrong Data Type Input in C++? In C++, when weâre expecting a certain type of input but the user enters a different type then such input is called incorrect data input and it can cause issues like unexpected behavior, program failures, or inaccurate results. In this article, we will learn how to handle wrong data type input in C+
2 min read
How to Validate User Input in C++ Validating the user input is very important for any program to ensure that the data being processed is correct and meaningful. In C++, various techniques can be used to validate the user input and in this article, we will learn how to validate the user input in C++. Validate User Input in C++To vali
4 min read