Re: First C Program, Problems getting serial data
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 18 Apr 2006 23:04:24 +0100
Rod Pemberton wrote:
"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:lnzmiify9k.fsf@xxxxxxxxxxxxxxxxxx
"Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxx> writes:or"Keith Thompson" <kst-u@xxxxxxx> wrote in message[...]
news:lnk69ohw43.fsf@xxxxxxxxxxxxxxxxxx
Personally, I use !, and other operations that act on boolean values,Determining if strings are equal or different is useful. However,
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 the last compared character in one string was above (>0)
heavilybelow (<0) the last compared character in the other, is useless andcharacterdependent on the nonportable and sometimes random ordering of thebooleanset. Because of this, I'd posit that strcmp() is _essentially_ afunction: 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.
not toAn explicit comparison, (strcmp(cmd, cmdcheck) == 0) makes it muchUntrue. There is an entire generation of programmers who were taught
clearer what's actually going on.
ofthink in terms of 'true'. They were taught to think entirely in termsall'false'. 'true' is thought of as 'not false' and 'false' is zero (forAnbut a few languages, i.e., ADA which, IIRC, Keith has experience in...).Sinceif() statement is thought of as 'if(not false)' or 'if(not zero)'.whichstrcmp() returns zero for equality, if(!strcmp()) says 'if(not zero)''if(0is understood to be 'true'. if(strcmp()==0), on the other hand saysis equal to 0)'. This, of course, lacks the simplicity, elegance, andYes, in C zero is false, and any non-zero value is true, so a boolean
mnemonic of 'if(not zero)'.
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.
booleanYes, this is largely a matter of style, and plenty of smart peopleTrue. They are not programming in ADA and don't need to separate
prefer the !strcmp() form.
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
.
- References:
- First C Program, Problems getting serial data
- From: Chuck Faranda
- Re: First C Program, Problems getting serial data
- From: regis
- Re: First C Program, Problems getting serial data
- From: Burton Samograd
- Re: First C Program, Problems getting serial data
- From: Simon Biber
- Re: First C Program, Problems getting serial data
- From: Burton Samograd
- Re: First C Program, Problems getting serial data
- From: Keith Thompson
- Re: First C Program, Problems getting serial data
- From: Rod Pemberton
- Re: First C Program, Problems getting serial data
- From: Keith Thompson
- Re: First C Program, Problems getting serial data
- From: Rod Pemberton
- First C Program, Problems getting serial data
- Prev by Date: Re: Different code behaviour in Unix & Windows
- Next by Date: Re: bad programming practice?
- Previous by thread: Re: First C Program, Problems getting serial data
- Next by thread: Re: First C Program, Problems getting serial data
- Index(es):
Relevant Pages
|
Loading