/*
* demonstrate integer overflow with multiplication
*
* Matt Bishop, ECS 36A
* April 4, 2024 original version
*/
#include <stdio.h>
int 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 2, 2024 at 12:13PM
|
You can get the raw source code here. |