/* * demonstrate integer overflow with multiplication * * Matt Bishop, ECS 36A * April 4, 2024 original version */ #includeint main(void) { int ii, jj, kk; /* used for integer multiplication */ long int i, j, k; /* used for long multiplication */ /* compute the product using longs */ /* there is no integer overflow here */ i = 222222222L; j = 555555555L; k = i * j; /* compute the product using ints */ /* there is integer overflow here */ ii = 222222222; jj = 555555555; kk = ii * jj; /* now show the difference */ printf("The product should be %ld\n", k); printf("but with integers, it overflows: %d\n", kk); /* th-th-that's all, folks! */ return(0); }
|
ECS 36A, Programming & Problem Solving Version of April 8, 2024 at 9:34AM
|
You can get the raw source code here. |