Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!midway!mimsy!rlgvax!scc From: scc@rlgvax.Reston.ICL.COM (Stephen Carlson) Newsgroups: comp.lang.c Subject: Re: "str*cmp()" ain't Boolean (was Re: v18i058: mush - Mail User's Shell, Part01/22) Message-ID: <1991May7.012817.4395@rlgvax.Reston.ICL.COM> Date: 7 May 91 01:28:17 GMT References: <1991Apr24.220750.15603@robobar.co.uk> <20523@ogicse.ogi.edu> <1991May1.181910.28750@robobar.co.uk> <7609@auspex.auspex.com> Reply-To: scc@rlgvax.Reston.ICL.COM (Stephen Carlson) Organization: International Computers Limited, Reston, Virginia, USA Lines: 42 In article <7609@auspex.auspex.com> guy@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@rlgvax.reston.icl.com | 11490 Commerce Park Drive | practice are the same. ..!uunet!rlgvax!scc | Reston, VA 22091 | (703) 648-3300