mult.c

/*
 * demonstrate integer overflow with multiplication
 *
 * Matt Bishop, ECS 36A
 * April 4, 2024	original version
 */
#include 

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);
}

UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 8, 2024 at 9:34AM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh