I am a beginner in The C programming language and was writing a program which shows integer overflow in action, here is the code below:
#include <stdio.h>
#include <limits.h>
int main(){
int n1;
int n2;
printf("enter a number: ");
scanf("%d", &n1);
printf("enter a number: ");
scanf("%d", &n2);
printf("\n%d\n", n1*n2);
return 0;
}
As you may be able to tell it takes two numbers from the user and times them together and outputs the value to the user, I was working with this program and input a value of 200000000000 x 200000000000, this gave me the output of -922746880. My question is that why do i get a random negative value such as this and what is the math and calculations behind it? so i can get a better understanding.
So far i am aware that the maximum value of the integer data type is 2,147,483,647 for a 32-bit cpu. But when 200000000000 x 200000000000 is being calculated, what happens to each digit calculated and why is it negative?
P.S can somebody please explain the math and behind the scene number action going on so i can see what actually happens for this value to be displayed.