Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!mouse From: mouse@thunder.mcrcim.mcgill.edu (der Mouse) Newsgroups: comp.lang.c Subject: Re: strcmp Message-ID: <1991Jun22.135109.26901@thunder.mcrcim.mcgill.edu> Date: 22 Jun 91 13:51:09 GMT References: <2695@m1.cs.man.ac.uk> <1991Jun18.074029.12226@panix.uucp> Organization: McGill Research Centre for Intelligent Machines Lines: 23 In article <1991Jun18.074029.12226@panix.uucp>, yanek@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@larry.mcrcim.mcgill.edu