Monthly Archives: February 2014

For a recent Uni project, I had to check user input for erroneous inputs. Since the program was adding together two integers, I needed to determine whether the resulting integer didn’t overflow – that is to say that the number is outside the range that can be represented by the computer. After some thinking, I settled on the following:

The first check is for positive numbers, and the second is for negative, so this is a catch-all solution. The MAX_INT was defined as 2147483647 using the #define compiler statement. It works because it checks whether the first number is greater than the largest representable number minus the second number.