strcmp

Thomas Wang thomasw at hpcupt1.cup.hp.com
Wed Jun 19 10:46:04 AEST 1991


> Are there any  examples of source code for strcmp, I need to implement
> code for a large variety of types in the style of strcmp and
> would like to see what has been done before.

> I know this seems like a simple school exercise, but I would like to see
> various implementations.

In my opinion, the +1, 0, and -1 comparisons are flawed, because they do not
take account unordered comparisons.  So I usually define 6 macros.

#define COMP_GREATER   (1)
#define COMP_EQUAL     (0)
#define COMP_LESS      (-1)
#define COMP_UNORDERED (MININT)

#define GE(x) ((x) >= COMP_EQUAL)  /* greater than or equal */
#define LE(x) (-(x) >= COMP_EQUAL) /* less than or equal, -MININT == MININT */

This system is compatible with strcmp(), yet able to deal with unordered
comparison.  For example, compare 1.0 against NaNs should return
COMP_UNORDERED.

int32 intcmp(int a, int b)
{
   return((a == b) ? COMP_EQUAL : ((a > b) ? COMP_GREATER : COMP_LESS));
}

 -Thomas Wang
              (Everything is an object.)
                                                     wang at hpdmsjlm.cup.hp.com
                                                     thomasw at hpcupt1.cup.hp.com



More information about the Comp.lang.c mailing list