"str*cmp()" ain't Boolean (was Re: v18i058: mush - Mail User's Shell, Part01/22)

Stephen Carlson scc at rlgvax.Reston.ICL.COM
Tue May 7 11:28:17 AEST 1991


In article <7609 at auspex.auspex.com> guy at auspex.auspex.com (Guy Harris) writes:
>
> >> } >   if (show_to && strncmp(p, login, strlen(login)))
> >
> >> My copy says !strncmp(p, login, strlen(login)) ....
> >
> >Ooops.  Finger trouble.  So does mine.
>
>Perhaps this is a hint that one shouldn't treat "strncmp()" as if it
>were a Boolean function, and should instead compare its result with 0
>explicitly, e.g. if you're comparing strings for equality, use "=="?

I prefer to set up the following macros:

#define streq !strcmp
#define strneq !strncmp

This way I can use streq() as a normal boolean-type function; that is, I can
use it bare in conditionals or with the logical negation '!', and it still makes
sense.  Compare,

	if (!strcmp("foo", "bar")) ...
with
	if (streq("foo", "bar")) ...

The latter is understandable at a glance; I wish I could say the same for
the former.  When I happen to care whether a string is greater than another,
I would use strcmp() with an explicit test:

	if (strcmp("foo", "bar") > 0) ...

Comments anyone?  Especially regarding streq's alternative definition:

#define streq(s,t) (strcmp((s), (t)) == 0)

Is it more kosher to define streq as a macro with two arguments rather than
one without any arguments?

-- 
Stephen Carlson           | ICL OFFICEPOWER Center    | In theory, theory and
scc at rlgvax.reston.icl.com | 11490 Commerce Park Drive | practice are the same.
..!uunet!rlgvax!scc       | Reston, VA  22091         |    (703) 648-3300



More information about the Comp.lang.c mailing list