Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!sri-spam!ames!ucbcad!ucbvax!decvax!tektronix!tekcrl!tekchips!barts From: barts@tekchips.UUCP Newsgroups: comp.lang.c Subject: Re: *\"LDA\" ok? Message-ID: <1623@tekchips.TEK.COM> Date: Sat, 22-Aug-87 21:31:48 EDT Article-I.D.: tekchips.1623 Posted: Sat Aug 22 21:31:48 1987 Date-Received: Sun, 23-Aug-87 18:35:56 EDT References: <8877@brl-adm.ARPA> <8088@mimsy.UUCP> Reply-To: barts@tekchips.UUCP (Bart Schaefer) Organization: Tektronix, Inc., Beaverton, OR. Lines: 62 In article <8088@mimsy.UUCP> chris@mimsy.UUCP (Chris Torek) writes: ]In article <8877@brl-adm.ARPA> ADLER1%BRANDEIS.BITNET@wiscvm.wisc.EDU writes: ]>I was trying to write a C program that would read MIX commands from ]>stdin. I also wanted to be able to verify that the string opcode ]>was actually internally equal to the string LDA.... ]> if (*opcode == *"LDA") printf("Gotcha!\n"); ]> else printf("No match...\n"); ]No doubt this has already been answered in mail directed to ]adler1@brandeis.bitnet, but I want to expand on this a bit. ] [...] ]To compare the characters in `opcode' with the string "LDA" for ]equality, one should use ] if (strcmp(opcode, "LDA") == 0) ]which is such a common idiom that old-time C programmers understand ]it at a glance. It seems to come late to neophyte programmers, ]though, and it seems reasonable to ask why. ] ]Perhaps it is because other languages provide string comparison within ]the language itself: ] [...examples deleted...] ]Eventually it seems to dawn upon these programmers that ] "LDA" ]generates an anonymous character array holding the letters L, D, ]A, and NUL (\0) and evaluates to the address of this array. Then ]the purpose of strcmp() becomes clear, and they live happily ever ]after :-). ] ]All I want to know is this: Why does it take so long for some ]programmers to see this, and how can we speed up the process? I got my first C experience about 3 years ago when I was handed a code fragment containing all sorts of marvelous UN*X ioctl() and fork()/wait() calls and told to turn it into an interactive editor/parser. Since then I have (hopefully) improved in my understanding of C and my programming style, but my introoduction to C is recent enough that I can comment on strcmp(). The single greatest problem I had in learning to use strcmp() is its return of 0 on "equality" of the strings. I was expecting a boolean-valued comparison, and this apparent sense-reversal (false on equality) threw more monkey wrenches into my early programs than I would ever have believed. Perhaps inexperienced programmers resort to trying direct comparisons ala string1 == string2 or *string1 == *string2 after a few failures of if (strcmp(string1,string2)) print("They match!\n"); to do what they expect. I'm not sure how to speed up learning the right way to use strcmp(). Maybe inexperienced programmers should be encouraged to use something like #define streq(s1,s2) (strcmp(s1,s2) == 0) until they get used to non-boolean-valued comparisons. I suppose, however, that it could be argued that this will only delay understanding strcmp(), but at least the novice will have a "function" that does what built-in equivalency tests in other languages already do. ]-- ]In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) ]Domain: chris@mimsy.umd.edu Path: seismo!mimsy!chris -- Bart Schaefer Oregon Graduate Center ...!tektronix!ogcvax!schaefer Guest at Tekchips ...!tektronix!tekchips!barts