strcmp

der Mouse mouse at thunder.mcrcim.mcgill.edu
Sat Jun 22 23:51:09 AEST 1991


In article <1991Jun18.074029.12226 at panix.uucp>, yanek at panix.uucp (Yanek Martinson) writes:
> For intcmp(a,b) why not just use a-b ?

In a word, overflow.

> That will tell you if they are equal and if not, which is greater.

Only when a-b doesn't overflow.  On a machine with 16-bit
two's-complement ints, consider a = -32000 and b = 32000.  Then a < b
(I trust nobody disagrees with this :-), so we want a negative result.
Unfortunately a-b overflows.  If it actually produces a result (as
opposed to a crash at run-time), the most likely thing to get, I would
say, would be 1536, which is not exactly negative.

> For strcmp(s,t): while(*s++==*t++&&*s&&*t); return *s-*t;

I think this has been sufficiently dissected already, so I'll skip it.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list