int strcmp(const char *s, const char *t) {
- int diff = 0; int i;
- for (i = 0; s[i] == t[i]; ++i)
if (s[i] == '\0')
return (0);
- return((int) (s[i] - t[i]));
- for (i = 0; 1; ++i) {
diff = s[i] - t[i];
if (diff || s[i] == '\0')
break;
- }
- return(diff);
}
Neither the old nor the new code is correct: you're supposed to do the comparison as unsigned char, so that e.g. strcmp("\x01", "\x81") is negative.
If char is a signed type (and int is more bits than char ;-) ) this code does the wrong thing.
Segher