Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!husc6!necntc!ima!haddock!karl From: karl@haddock.UUCP (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Style question re common C expression. Message-ID: <608@haddock.UUCP> Date: Mon, 22-Jun-87 20:54:14 EDT Article-I.D.: haddock.608 Posted: Mon Jun 22 20:54:14 1987 Date-Received: Wed, 24-Jun-87 03:58:45 EDT References: <4934@utcsri.UUCP> <8164@utzoo.UUCP> Reply-To: karl@haddock.ISC.COM.UUCP (Karl Heuer) Organization: Interactive Systems, Boston Lines: 28 Keywords: STREQ In article <8164@utzoo.UUCP> henry@utzoo.UUCP (Henry Spencer) writes: >> ... like the standard "#define STREQ(s,t)(strcmp(s,t) == 0)"? > >This is slightly off the topic, but note two things: > >First, that should be "(strcmp((s),(t)) == 0)" for safety's sake, although >in this case trouble is unlikely. Unlikely? I think in this case it's impossible. But you're right in that it's a good habit to get into, and it certainly doesn't hurt. >Second, if you don't indulge in the perversion of routinely using side >effects within expressions, then a still better form is: > >#define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) Some people would phrase that the other way around: "If you don't indulge in the perversion of writing macros that evaluate their arguments more than once, then you can write f(p++), using them just like functions." >This makes a considerable speed difference in programs that use strings >a lot, since string comparisons usually fail on the very first character. Of course, a really good compiler will make "strcmp" a builtin, giving you the same speed improvement without sacrificing any of the calling contexts. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint (Btw, the name "STREQ" isn't all that standard. I spell it "streq", myself.)