Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: standard error codes Date: Wed, 11 Jan 2017 12:03:01 -0800 Organization: None to speak of Lines: 58 Message-ID: References: <96e16709-18eb-4399-86c4-a6f30a767df3@googlegroups.com> <212f08fd-b09e-4848-8485-f7480eaaae45@googlegroups.com> <86260b21-0fa2-42b4-93c6-08e9241d4c17@googlegroups.com> <64011b84-2f8d-44f2-902a-0eacfcdf630b@googlegroups.com> <11823fb6-613e-4283-95e8-dd930e152418@googlegroups.com> <546841f1-b478-44dc-abc0-22ca4f46e553@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="773e6a95afbb07ee2983b9f78b62bfd0"; logging-data="5289"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+XnUVgNNwNhgXHZj+J+KvS" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:rDcOIGRbWwtrJG6b/PRzZK8eH6U= sha1:weY2NGG8wRctZ4M1By/GFhnu6TQ= Xref: csiph.com comp.lang.c:101449 BartC writes: > On 11/01/2017 18:25, Richard Heathfield wrote: [...] >> Yes, I'd agree with that. We don't need a streq function. What's wrong >> with strcmp? > > Because 99% of all calls to strcmp use it to compare for string equality? And why is that a problem? In the early days, it turned out to be just as easy to provide a string comparison function as a slightly less flexible string equality function. It ain't broke. Why fix it? > I've looked at some codebases and the overwhelming use of strcmp seems > to either use strcmp(s,t)==0, or !strcmp(s,t). Neither looks obviously > like a test for equality. They certainly do to any C programmer who's familiar with what strcmp() does. > In one or two places, I saw a call like this: if (strcmp(s,t)), which is > even less intuitive (I think it tests whether s is not equal to t). Yes, that's what it does (you're not sure?). I wouldn't write it that way myself, but it's perfectly understandable *if* you understand what strcmp() does. > With streq, those first calls would look like this: > > if (streq(s,t)) > > and the second like this: > > if ( !streq(s,t)) > > In theory, because could create a streq-like function using macros or a > wrapper, but someone neglected to tell the people who wrote this code! Apparently the people who wrote the code didn't feel the need to write a streq() macro, because they felt, as I do, that calling strcmp() directly is clear enough. You seem to be assuming that, if the authors of the code you're looking at *had only known* that they could wrap strcmp() in a macro, then of course they would have done so to make their code clearer. I disagree. (BTW, the name "streq" is reserved. If I felt the need for such a macro, I might call it "STREQ".) > It doesn't happen. Not often, but it can happen in your code if you want it to. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"