Newsgroups: comp.lang.c Path: utzoo!henry From: henry@zoo.toronto.edu (Henry Spencer) Subject: Re: strcmp Message-ID: <1991Jun18.153653.1494@zoo.toronto.edu> Date: Tue, 18 Jun 1991 15:36:53 GMT References: <2695@m1.cs.man.ac.uk> <1991Jun18.074029.12226@panix.uucp> Organization: U of Toronto Zoology In article <1991Jun18.074029.12226@panix.uucp> yanek@panix.uucp (Yanek Martinson) writes: >For intcmp(a,b) why not just use a-b ? That will tell you if they are equal >and if not, which is greater. If the subtraction doesn't overflow, that is. (The effects of overflow are undefined, and strange things can happen even with old compilers.) >For strcmp(s,t): while(*s++==*t++&&*s&&*t); return *s-*t; Apart from being inefficient, this code is wrong, since the final comparison must be done as if characters are unsigned. (This is not precisely the rule I would have picked, but at least ANSI C did pick something specific...) That's aside from the fact that the final comparison is being done on the two characters *after* the mismatch rather than on the mismatch itself... -- "We're thinking about upgrading from | Henry Spencer @ U of Toronto Zoology SunOS 4.1.1 to SunOS 3.5." | henry@zoo.toronto.edu utzoo!henry