Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsh!hoswjc!wjc From: wjc@hos1cad.ATT.COM (Bill Carpenter) Newsgroups: comp.lang.c Subject: Re: "str*cmp()" ain't Boolean (was Re: v18i058: mush - Mail User's Shell, Part01/22) Message-ID: <1991May4.181718.12420@cbnewsh.att.com> Date: 4 May 91 18:17:18 GMT References: <1991Apr24.220750.15603@robobar.co.uk> <20523@ogicse.ogi.edu> <1991May1.181910.28750@robobar.co.uk> <7609@auspex.auspex.com> Sender: bill@cbnewsh.att.com (william.j.carpenter) Followup-To: comp.lang.c Organization: AT&T Bell Laboratories Lines: 21 In-Reply-To: guy@auspex.auspex.com's message of 3 May 91 21: 17:36 GMT guy> Perhaps this is a hint that one shouldn't treat "strncmp()" as if guy> it were a Boolean function, and should instead compare its result guy> with 0 explicitly, e.g. if you're comparing strings for equality, guy> use "=="? Since most of the time I'm using str*cmp() in a Boolean way anyhow, and since I used to frequently get the sense of the test reversed, I almost always do it as a macro now: #define EQSTR(a,b) ((a) && (b) && (*(a) == *(b)) && !strcmp((a),(b))) #define EQSTRN(a,b,n) ((a) && (b) && (*(a) == *(b)) && !strncmp((a),(b),(n))) Of course, these are a little elaborate and have side-effects, so you could stick with the simpler #define EQSTR(a,b) (!strcmp((a),(b))) #define EQSTRN(a,b,n) (!strncmp((a),(b),(n))) (especially if you enjoy dereferenced nulls as much as I do :-). -- Bill Carpenter att!hos1cad!wjc or attmail!bill