[H-GEN] C programming idioms

Ted Percival ted at midg3t.net
Sat Feb 17 23:57:40 EST 2007


bjf at bjf.id.au wrote:
> My question to the collective consciousness is, what resources do the
> professional C developers here use as tutorials and references to get
> this more advanced knowledge that you typically don't find in the "Teach
> Yourself C for Dummies in 24 Hours" kind of books?

I've found reading and modifying source to be the best way, particularly
 that of free software projects. Fixing bugs in various different
projects is a good way to find out what does and doesn't make code
maintainable. Architectural documentation is always valuable for
somebody new to the codebase. No doubt you're aware of the value of
commenting your code.

On a more technical level it occurred to me that the practice of
declaring functions static is fairly common but not self-evident in what
it does. It makes the function declaration visible only at file scope
(and within). The benefits are that it does not need an entry point in
the symbol lookup table and that the compiler can inline it, knowing it
will not be called by any external code.

Of course there's "goto considered harmful". I was initially surprised
to see how often it is still used, but it can be useful to clean up
resources allocated within a function, particularly on an error path. My
experience with this is that it is preferable when the alternative is
many levels of 'if' branching, with each one only determining whether a
function call succeeded. In these cases they're really poor men's (C
coders') exceptions.

With C, it makes a lot of sense to know about how the code will be
executed once it's compiled. I don't mean for micro-optimisation, but
knowing how the stack grows, keeping in mind that you're dealing with a
linear address space and that an array is really just a pointer, and
that a pointer just points to a bit or memory and you can interpret the
memory however you like.

I've never had much interest in technical books, but you could probably
find some a helpful reference if you look at publishers of more advanced
material (eg. O'Reilly publications) rather than the first-timer guides
(eg. Teach Yourself Foo in 24 Hours). That said, I've never read an
O'Reilly book so maybe they're not actually any different.

Have fun!

-- 
tedp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 252 bytes
Desc: OpenPGP digital signature
URL: <http://lists.humbug.org.au/pipermail/general/attachments/20070218/02e47976/attachment.sig>


More information about the General mailing list