Best book on C gotchas and best practices?

Discussion in 'C Programming' started by Chicken McNuggets, Jul 31, 2013.

  1. 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.
     
    Chicken McNuggets, Jul 31, 2013
    #1
    1. Advertisements

  2. 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
     
    Andreas Perstinger, Jul 31, 2013
    #2
    1. Advertisements

  3. 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.
     
    Chicken McNuggets, Jul 31, 2013
    #3
  4. 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.
    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.
     
    Ben Bacarisse, Jul 31, 2013
    #4
  5. Chicken McNuggets

    Eric Sosman Guest

    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).
     
    Eric Sosman, Aug 1, 2013
    #5
  6. Koenig C Traps and Pitfalls
    van der Linden Expert C Programming: Deep C Secrets
     
    Chris Riesbeck, Aug 1, 2013
    #6
  7. It's not a constraint violation. It's probably undefined behavior.
     
    Keith Thompson, Aug 1, 2013
    #7
  8. Chicken McNuggets

    Eric Sosman Guest

    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!
     
    Eric Sosman, Aug 2, 2013
    #8
  9. 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>
     
    David Thompson, Aug 5, 2013
    #9
  10. Perhaps:
    Portability and the C Language by Rex Jaeschke
    ---
    Fred J. Tydeman Tydeman Consulting
    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.
     
    Fred J. Tydeman, Aug 5, 2013
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.