compiler detecting divide by zero

Ken Lerman lerman at stpstn.UUCP
Wed Nov 28 02:21:42 AEST 1990


In article <36233 at cup.portal.com> ts at cup.portal.com (Tim W Smith) writes:
->For obscure reasons that I won't go into, I wanted a divide
->by zero in a program.  I was compiling on SCO Unix System V/386
->relase 2.2 (I think...) with whatever C compiler comes with
->this version of Unix.
->
->I tried the obvious:
->
->	int	i, j;
->
->	i = 1;
->	j = 0;
->	i/=j;
->
->The compiler caught this.  Grrr.  Next I tried:
->
->	i = 1;
->	j = 1;
->	i -= 1;
->	j/=i;
->
->It still caught it.  Double grrr!
->
->Next try:
->
->	int	i, j, *p;
->
->	i = 1;
->	j = 1;
->	p = &i;
->	j /= *p - 1;
->
->It still caught it!
->
->This one got past it:
->
->	j = 5;
->	for ( i = 0; i < 5; i++ )
->		j--;
->	i /= j;
->
->After the previous try, I was a bit surprised that it didn't
->figure out this one!
->
->						Tim Smith

I haven't tried it, but I'll bet that:

    int divide(int a, int b){ return a/b; }

will cause your divide error when called by:

    x = divide(1,0);

Of course, if your compiler is brilliant, you might have to put the
function divide in a separate file. :-)

Ken



More information about the Comp.lang.c mailing list