Re: First C Program, Problems getting serial data



Rod Pemberton wrote:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:lnzmiify9k.fsf@xxxxxxxxxxxxxxxxxx
"Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxx> writes:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:lnk69ohw43.fsf@xxxxxxxxxxxxxxxxxx
[...]
Personally, I use !, and other operations that act on boolean values,
only for values that really are boolean. The strcmp() function yields
one of three possible kinds of result: <0, ==0, >0. If its name were
"strings_differ(), I'd happily write (!strings_differ(cmd, chdcheck)),
but since the name of the function doesn't indicate a boolean result,
I find it a bit confusing to treat it as if it did.
Determining if strings are equal or different is useful. However,
determining if the last compared character in one string was above (>0)
or
below (<0) the last compared character in the other, is useless and
heavily
dependent on the nonportable and sometimes random ordering of the
character
set. Because of this, I'd posit that strcmp() is _essentially_ a
boolean
function: zero equal, non-zero if different.
strcmp() gives a consistent ordering of string values. Sometimes an
internally consistent ordering is all you need, for example if you
want to build a search tree.

strcmp() is not a boolean function.

I never said it was. I said it is _essentially_ a boolean function. That's
because ordering of string values is a very low use situation...

No, it is essentially a comparison function with three return values, something I have a lot of use for. It is often used in a boolean context where two of those values are treated the same.

An explicit comparison, (strcmp(cmd, cmdcheck) == 0) makes it much
clearer what's actually going on.
Untrue. There is an entire generation of programmers who were taught
not to
think in terms of 'true'. They were taught to think entirely in terms
of
'false'. 'true' is thought of as 'not false' and 'false' is zero (for
all
but a few languages, i.e., ADA which, IIRC, Keith has experience in...).
An
if() statement is thought of as 'if(not false)' or 'if(not zero)'.
Since
strcmp() returns zero for equality, if(!strcmp()) says 'if(not zero)'
which
is understood to be 'true'. if(strcmp()==0), on the other hand says
'if(0
is equal to 0)'. This, of course, lacks the simplicity, elegance, and
mnemonic of 'if(not zero)'.
Yes, in C zero is false, and any non-zero value is true, so a boolean
value should be used directly as a condition, not compared for
equality to either true or false. Something like "if (b == true)" is
both dangerous and unnecessarily verbose; "if (b == false)" happens
not to be as dangerous (because there's only a single false value),
but it's just as unnecessarily verbose. They should be "if (b)" and
"if (!b)", respectively.

But that applies only when b is a boolean value. The result returned
by strcmp() is not.

(Aside: I've never heard of a language called "ADA". I've programmed
in Ada. It's a name, not an acronym.)

True enough, although it is frequently found all capitalized.

That doesn't make it correct.

Yes, this is largely a matter of style, and plenty of smart people
prefer the !strcmp() form.
True. They are not programming in ADA and don't need to separate
boolean
false and zero as distinct values.
Which has no relevance to what we're actually discussing.

Yes, it does. That is where you're perspective originates and is why you
have 'clarity' issues with treating the result of strcmp as a boolean.

I spent a lot of my early career as an assembler programmer, and comparing strings I would naturally end up with something that behaved like strcmp since comparing two bytes gave that information on the processors I was using. So I come from a background of not having a boolean.

Like Keith I find it easier to read/write comparing strcmp with 0 than using it as a boolean. I can read it used as a boolean, I just have to spend an extra couple of seconds interpreting it.

If you want to argue that it's acceptable to treat the result of
strcmp() as a boolean value (true if the arguments are unequal, false
if they're equal), that's not an entirely unreasable argument. I
don't happen to agree with it (if strcmp() were intended to be boolean
it would have a different name), but I recognize that some programmers
either value terseness over clarity, or just don't find !strcmp(x, y)
as unclear as I do.

just don't find !strcmp(x, y) as unclear as I do.

Why do you find this unclear? If you treat 1 and -1 as 'not false' and 0 as
false, it's completely clear.

It is unambiguous but for some it takes longer to read than a comparison with 0.

Bringing in an irrelevant argument about true and
false doesn't help your case.

Irrelevant? I tried to explain things, when _you_ said you found things
unclear:

Some people find that reading "if (strcmp(x,y))" or "if (!strcmp(x,y))" takes a little though where comparing against 0 doesn't because they think of it as returning three values, not returning a boolean that is the wrong way up.

but since the name of the function doesn't indicate a boolean result,
I find it a bit confusing to treat it as if it did.

The discussion regarding true and false describes why others don't find it
unclear. If you spent time to comprehend, perhaps it would help your
clarity.

I'm sure Keith fully understands how boolean context work in C.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
.



Relevant Pages

  • Re: First C Program, Problems getting serial data
    ... only for values that really are boolean. ... I'd posit that strcmp() is _essentially_ a boolean ... zero equal, non-zero if different. ... There is an entire generation of programmers who were taught not to ...
    (comp.lang.c)
  • Re: First C Program, Problems getting serial data
    ... only for values that really are boolean. ... strcmp() gives a consistent ordering of string values. ... There is an entire generation of programmers who were taught ...
    (comp.lang.c)
  • Re: Coding standards
    ... >> suggests to me that the person who wrote it doesn't understand boolean ... >> variables and the C idiom that was established with the C standard ... What do you think strcmp stands for? ... name implies that it tests whether the arguments differ. ...
    (comp.lang.c)
  • Re: using "!!" in "c"
    ... > its results as boolean. ... strcmp is more similar to a compar function, ... For pseudoboolean expressions like isspace, ...
    (comp.lang.c)
  • Re: Why INFINITE loop in a thread occupy so much CPU time??
    ... Using integer types instead of a boolean type is essentially slopping programming. ... It is habit that too many bad C programmers seem to develop, ... K&R C really was an apalling language. ...
    (microsoft.public.vc.mfc)

Loading