Best book on C gotchas and best practices?

C

Chicken McNuggets

I'm pretty up to speed with C but I still feel there are areas that I
make mistakes in my design and implementation. Would anyone recommend
the best book that deals with these kinds of issues? Obviously I already
have K&R, and the C11 standard document for reference but I was looking
for something a bit more focused on how to avoid common mistakes or
anti-patterns.

Any help is appreciated.
 
A

Andreas Perstinger

Obviously I already have K&R, and the C11 standard document for
reference but I was looking for something a bit more focused on how
to avoid common mistakes or anti-patterns.

The only book I'm aware of is the rather old (from 1989) "C Pitfalls and
Traps" by Andrew Koenig.

You can find his paper on which the book is based on here:
http://literateprogramming.com/ctraps.pdf

Bye, Andreas
 
C

Chicken McNuggets

The only book I'm aware of is the rather old (from 1989) "C Pitfalls and
Traps" by Andrew Koenig.

You can find his paper on which the book is based on here:
http://literateprogramming.com/ctraps.pdf

Bye, Andreas

Thanks. I'll check it out. I assume that should be ANSI compliant if it
was released in 1989.

Luckily C is one of the few languages where age isn't really much of a
concern.
 
B

Ben Bacarisse

Chicken McNuggets said:
Thanks. I'll check it out. I assume that should be ANSI compliant if
it was released in 1989.

Not really. It's definitely showing its age.

It describes some things that are problems with pre-ANSI C (the =+ and
related operators for example) and it includes some things as worries
without (understandably) giving the modern take on them. For example,
he says "in function calls, [...] most compilers do not check argument types".
That was probably true in 1989, but ANSI-C offered a solution other than
being careful (function prototypes) and C99 and later essentially
mandated such checking.

I could not find a discussion of anything that is specifically ANSI-C.
For example, function prototypes have their own pitfalls but
these are not discussed. Some examples show malloc returning char *
because there was no void * at the time. String literals and considered
modifiable, and so on.
Luckily C is one of the few languages where age isn't really much of a
concern.

An updated version would be more useful. You need to know what to
ignore and what is now plain wrong about ANSI-C and later.
 
E

Eric Sosman

I'm pretty up to speed with C but I still feel there are areas that I
make mistakes in my design and implementation. Would anyone recommend
the best book that deals with these kinds of issues? Obviously I already
have K&R, and the C11 standard document for reference but I was looking
for something a bit more focused on how to avoid common mistakes or
anti-patterns.

Andrew Koenig's "C Traps and Pitfalls" is worth a lukewarm
recommendation. "Lukewarm" for two reasons:

- First and more important, it's aimed at people making the
transition from pre-Standard to ANSI C, and is nowadays
rather dated.

- Second and less important, it goes out of its way to say
that the printf() specifier "%8%" is well-defined (even in
the original ANSI Standard it was a constraint violation).
 
C

Chris Riesbeck

I'm pretty up to speed with C but I still feel there are areas that I
make mistakes in my design and implementation. Would anyone recommend
the best book that deals with these kinds of issues? Obviously I already
have K&R, and the C11 standard document for reference but I was looking
for something a bit more focused on how to avoid common mistakes or
anti-patterns.

Any help is appreciated.

Koenig C Traps and Pitfalls
van der Linden Expert C Programming: Deep C Secrets
 
K

Keith Thompson

Eric Sosman said:
Andrew Koenig's "C Traps and Pitfalls" is worth a lukewarm
recommendation. "Lukewarm" for two reasons:

- First and more important, it's aimed at people making the
transition from pre-Standard to ANSI C, and is nowadays
rather dated.

- Second and less important, it goes out of its way to say
that the printf() specifier "%8%" is well-defined (even in
the original ANSI Standard it was a constraint violation).

It's not a constraint violation. It's probably undefined behavior.
 
E

Eric Sosman

It's not a constraint violation. It's probably undefined behavior.

You're right: The "shall" is not in a constraints section,
so violating it yields undefined behavior. Sorry for the
misstatement, and thanks for the correction!
 
D

David Thompson

Chicken McNuggets <[email protected]> writes:
Not really. It's definitely showing its age.

It describes some things that are problems with pre-ANSI C (the =+ and
related operators for example) and it includes some things as worries
without (understandably) giving the modern take on them. For example,
he says "in function calls, [...] most compilers do not check argument types".
That was probably true in 1989, but ANSI-C offered a solution other than
being careful (function prototypes) and C99 and later essentially
mandated such checking.
Not really. C99 requires alll functions be *declared* (which C<=89 did
not) but it still allows non-prototype aka K&R1 declarations, and
bodies. And it still calls them obsolescent -- as C89 did.

<snip>
 
F

Fred J. Tydeman

I'm pretty up to speed with C but I still feel there are areas that I
make mistakes in my design and implementation. Would anyone recommend
the best book that deals with these kinds of issues?

Perhaps:
Portability and the C Language by Rex Jaeschke
---
Fred J. Tydeman Tydeman Consulting
(e-mail address removed) Testing, numerics, programming
+1 (775) 287-5904 Vice-chair of PL22.11 (ANSI "C")
Sample C99+FPCE tests: http://www.tybor.com
Savers sleep well, investors eat well, spenders work forever.
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top