Recommended style

C

Chris Croughton

With this, on the other hand, I agree. Inverted strcmp sense errors
are one of the more common sort I find in others' code.

I've made it a lot in my own. I do usually catch the fault before I've
even finished the statement, but it's one I've had to train myself to
catch.
I don't
remember making one myself since I adopted Peter van der Linden's
macro, which I think does improve readability significantly. For
example:

#define MySTRCMP(s1, op, s2) (strcmp(s1, s2) op 0)
if (MySTRCMP(password, ==, "drowssap"))

Inlining the operator brings string comparisons syntactically closer
to their numeric cousins.

Oh, I like that! Yes, that makes it a lot more obvious. I would
probably call the macro STRCOMPARE, but that's a matter of taste...

Chris C
 
M

Michael Wojcik

Oh, I like that! Yes, that makes it a lot more obvious.

I can't take credit for it - I saw it first in PvdL's _Expert C
Programming: Deep C Secrets_ (which I believe is mentioned in the
FAQ). I don't know if he invented it. (I just flipped through
the book to find where he mentions it; it's in a sidebar with
little additional info.)
I would
probably call the macro STRCOMPARE, but that's a matter of taste...

PvdL just called his version STRCMP. I generally use a two- or
three-character mixed-case prefix for all of my macros, which avoids
reserved identifiers (with block-capital names you can't have macro
names that start with "E") and helps avoid collisions with macros
defined in implementation headers and the like. (And even with
non-macro identifiers, in some cases - I've run into code that used
block-capital names for eg structure fields, and using them for
enum values is pretty common.)

--
Michael Wojcik (e-mail address removed)

Pogo: The dogs *scarcely* ever catches the rabbit.
Bun: "Scarcely" got a very unpleasant ring of frequency to it.
-- Walt Kelly
 
C

Chris Croughton

I can't take credit for it - I saw it first in PvdL's _Expert C
Programming: Deep C Secrets_ (which I believe is mentioned in the
FAQ). I don't know if he invented it. (I just flipped through
the book to find where he mentions it; it's in a sidebar with
little additional info.)

Ah, thanks, I'll look for that book.
PvdL just called his version STRCMP.

I'm currently working on a system where their library uses STRCMP,
STRCPY etc. for 16 bit (UCS2) versions of the functions, I have an
aversion to using the 'obvious' names. But I might just call it CMP...
I generally use a two- or
three-character mixed-case prefix for all of my macros, which avoids
reserved identifiers (with block-capital names you can't have macro
names that start with "E") and helps avoid collisions with macros
defined in implementation headers and the like.

And libraries, which are a real pain.
(And even with non-macro identifiers, in some cases - I've run into
code that used block-capital names for eg structure fields, and using
them for enum values is pretty common.)

Enum values are, as far as a number of coding standards are concerned,
the same as defined constants. Indeed, I've seen people describe enums
as "a sort of defined constant without the #define" in teaching C.

Chris C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,415
Latest member
SharonCran

Latest Threads

Top