I've recently been messing around with C and have come to the concept of integer overloading. I'm trying to create a program that detects whether or not two 32 bit integers multiplied by each other will be able to fit into another 32 bit integer.
I understand the concept of integer overflow and how integers end up overflowing, but I'm stuck with figuring out the logic for this program. Here's what I have so far:
int main() {
int min = INT_MIN;
int max = INT_MAX;
int num1, num2;
int x = num1 * num2;
printf("Please enter a number you would like to multiply: ");
scanf("%d", &num1);
printf("\nPlease enter a second number you would like to multiply: ");
scanf("%d", &num2);
if (num1 > max / num2){
printf("\nOverflow! - first case\n");
}
else if (num2 > max / num1){
printf("\nOverflow! - second case\n");
}
else if ((num1 > max - num2 && num2 > 0 ) ||
(num1 < max - num2 && num2 < 0)){
printf("\nOverflow! - third case\n");
}
else
printf("\nNot Overflow!\n");
return 0;
}
As you can see, my program can detect some cases for overflow, but several other cases like -2 * 3 and -2 * -3 will get caught by my conditions.
So what I have will absolutely not work. What are good ways to go about solving this?