Groups | Search | Server Info | Login | Register


Groups > comp.lang.c.moderated > #453

Re: Convert string to C expression syntax

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c.moderated
Subject Re: Convert string to C expression syntax
Date 2013-08-02 18:34 -0500
Organization None to speak of
Message-ID <clcm-20130802-0001@plethora.net> (permalink)
References <clcm-20130730-0006@plethora.net> <clcm-20130731-0004@plethora.net> <clcm-20130801-0002@plethora.net> <clcm-20130801-0003@plethora.net>

Show all headers | View raw


Ike Naar <ike@ukato.freeshell.org> writes:
> On 2013-08-01, Keith Thompson <kst-u@mib.org> wrote:
>> Barry Schwarz <schwarzb@dqel.com> writes:
>> [...]
>>> If you want to check whether the string has a certain value, you would
>>> use strcmp or strncmp in the condition of your if statement. Something
>>> along the lines of 
>>>      if (!strcmp(argv[1], "ABC")
>>>           {/* equal code */}
>>>      else
>>>           {/* optional not equal code */}
>>
>> To be clear (since the OP may not be aware of it), strcmp() returns a
>> value less than, equal to, or greater than 0 if the first argument is
>> respectively than, equal to, or greater than the right argument.
>>
>> So (!strcmp(argv[1], "ABC") is true if the string pointed to by argv[1]
>> is equal to "ABC".  If you treat the result of strcmp() as a boolean,
>> it's true of the strings are unequal, false if they're equal.
>>
>> Personally, I prefer to be more explicit; I'd write:
>>
>>     if (strcmp(argv[1], "ABC") != 0) ...
>>
>> but you'll commonly see the !strcmp() form in C code.
>
> Your preferred code does the opposite of the original code.

D'oh!

I'm tempted to say that this demonstrates the dangerous obscurity
of the !strcmp() form, so that even experienced programmers can get
it wrong, but I'll resist that temptation and just try to pay more
attention next time.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
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"
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line.  Sorry.

Back to comp.lang.c.moderated | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Convert string to C expression syntax nilay <nilaybhatt@gmail.com> - 2013-07-30 21:58 -0500
  Re: Convert string to C expression syntax James Kuyper <jameskuyper@verizon.net> - 2013-07-31 21:22 -0500
  Re: Convert string to C expression syntax Barry Schwarz <schwarzb@dqel.com> - 2013-07-31 21:22 -0500
    Re: Convert string to C expression syntax Keith Thompson <kst-u@mib.org> - 2013-08-01 12:58 -0500
      Re: Convert string to C expression syntax Ike Naar <ike@ukato.freeshell.org> - 2013-08-01 21:05 -0500
        Re: Convert string to C expression syntax Keith Thompson <kst-u@mib.org> - 2013-08-02 18:34 -0500
  Re: Convert string to C expression syntax gordonb.uvkjx@burditt.org (Gordon Burditt) - 2013-07-31 21:22 -0500
  Re: Convert string to C expression syntax Roberto Waltman <usenet@rwaltman.com> - 2013-08-05 11:24 -0500
  Re: Convert string to C expression syntax malcolm.mclean5@btinternet.com - 2013-09-02 04:09 -0500
  Re: Convert string to C expression syntax "Paul D. DeRocco" <pderocco@ix.netcom.com> - 2013-09-06 23:25 -0500
    Re: Convert string to C expression syntax James Kuyper <jameskuyper@verizon.net> - 2013-09-11 17:27 -0500
      Re: Convert string to C expression syntax "Paul D. DeRocco" <pderocco@ix.netcom.com> - 2013-09-12 11:34 -0500
      Re: Convert string to C expression syntax Francis Glassborow <francis.glassborow@btinternet.com> - 2013-09-12 11:38 -0500
  Re: Convert string to C expression syntax Nitin Tripathi <2nitintr@gmail.com> - 2013-09-11 17:31 -0500

csiph-web