Fog Creek Software
Discussion Board

Examples of well-written C?

I'm going to be writing a C library soon and was wondering if any of you could point me towards examples of what you consider to be well-written (i.e., works, is clean, well documented, etc.) C libraries or other pieces of software for me to emulate.


Stephen Van Devanter
Monday, October 06, 2003

Where not to look:

Rhys Keepence
Monday, October 06, 2003

How about the Standard C Library?

Simon Lucy
Monday, October 06, 2003

The GTK+ library:

Giovanni Corriga
Monday, October 06, 2003

I wouldn't recommend the standard C library at all... it is full of weirdness like strtok not being threadsafe and getchar returning an int only for the EOF case and whatnot.  It is not that intuitive to use, it's just that most people have gotten use to it.  It is also pretty incomplete.

I'm talking about the interfaces of course, I'm sure there are many implementations of varying code quality.

Monday, October 06, 2003


To be serious, I would suggest taking a peek at the Linux kernel source code. It is well written in my oppinion.

Monday, October 06, 2003

apache 2.x

Monday, October 06, 2003


Rob Mayoff
Monday, October 06, 2003

Io is a scripting language interpreter/VM. I think the code is very well written and cleanly modularized. I've studied the code for a number of scripting language interpreters/VMs, such as Perl, Ruby, and Python. They are extremely UGLY and (especially Ruby) do not have clean module interfaces.

Monday, October 06, 2003

> How about the Standard C Library?

I would think twice about mimicking the style of a standard library. The standard extends special priveleges to the implementors of the standard library, like the use of names that begin with underscores, which are restricted in application code.

Ken Dyck
Monday, October 06, 2003

Most of us had our managers and architects chase us down with copies of Code complete.

Li-fan Chen
Monday, October 06, 2003

What kind of library? If it's a widget set, GTK may be a good bet (never used it though, couldn't tell you). If its something more specific, look at SQLite ( ). It exposes a very simple API. If you're on unix, a well designed app that's divided into libraries that you may find instructive is mplayer: .

Each of those uses the libraries to a different purpose: gtk provides a boatload of functionality and exposes a big API. SQLite exposes < 10 functions and encapsulates a lot of complexity. Mplayer uses libraries for seperate drivers and codecs and such, with a whole bunch of big libraries communicating through a well defined API. I'm not sure that last is a good example of *library* code, but my small experiance with it has been positive, from a code perspective.

Mike Swieton
Monday, October 06, 2003

The Python interpreter
[ ]

Anything by Fabrice Bellard [ ], e.g.
tcc - a tiny C compiler [ ],
qemacs - a tiny emacs clone [ ],
qemu - a fast 386 dynamic recompiler (it recompiles 386 binary code to the architecture it's running on, on the file), that's nearly trivial to port to new processors.
TinyGL - a simple but fast OpenGL implementation

Many people would disagree with me, but ...
Anything by Dan Bernstein, e.g.
qmail - [ ]

If you're also interested in C++ code, check out
FLTK - [ ]
The QT library [ ] (You can only have the source for the X version for free, though).

Ori Berger
Monday, October 06, 2003

On Qt: it demonstrates a lot of good design, but I think it's worth noting that a lot of its powers come from a precomiler. That is, it operates by changing the language. I'm not certain this is a good way to teach good style.


Mike Swieton
Monday, October 06, 2003

Actually, I think it's a good style to learn from.

If the language doesn't provide what you need, add it to the language. Stroustroup (sp?) did it and got C++ (the first version of which, called "C with objects", was a very simple precompiler). More often, people do it with the standard preprocessor - if you examine code written by Arthur Whitney, you'll notice his C code looks a lot more like APL than it does like C.

Lisp makes this trivial - to the extent that if you've used this feature in Lisp, you often feel handicapped in other languages.

For small things, I consider it bad style - you don't write a 1000 line preprocessor to shorten something from 1000 lines to 200 lines. However, for important things, a custom language (even if implemented as a relatively simple preprocessor to an existing language) is often a good thing.

Consider, e.g., template metaprogramming in C++. Many people would disagree with me, but I think it's bad style; It abuses the fact that the template instantiator is Turing complete to extend the language, and in the way makes many other tools work with lesser ability (debugger, profiler, and .. even the compiler!). Yes, it's "standard" C++ - but it would have been about as simple to write a pre-processor instead of e.g. Blitz, and the result would work better.

On the other hand, ESQL (Embedded SQL) precompilers complicate things more often than not, and are a poor example of extending the language IMHO; So are AspetctC and AspectJ.

So, to sum up - sometimes, extending the language (through a preprocessor) _is_ an example of well written code. Sometimes it is not. Qt is an example of an extensive, well written language extension.

(Personally, I prefer FLTK, which works much closer to the "metal". But if you like Qt's abstraction level, I think it's a fine example)

Ori Berger
Tuesday, October 07, 2003

You might want to take a look at some of the code that comes out of the Merit group in Ann Arbor.  OpenLDAP and radiusd are the two I'm familiar with.

They aren't examples of good coding style. They're examples of what happens when coders live and work in a city where possession of marijuana is a slap on the wrist, and the loss of your pot will set you back more than fines and court costs.  In short, use these as examples of what not to do.  The code is horrible and a pain to sort out.  When you've been able to extend the functionality of one of those programs, you'll have an appreciation for clean code.

Clay Dowling
Tuesday, October 07, 2003

OK, that was funny.

Clutch Cargo
Wednesday, October 08, 2003

Anything from This guy has a DJB-spirit in his code without the NIH-syndrome. Check out his libowfat or his embedded utilities.

Jonas B.
Saturday, October 11, 2003

runtime, can you elaborate on what is so UGLY
with the python and Ruby interpreters? Even
Larry Wall agrees that perl sourcecode is an
horror, so I won't question _that_ ;-)

Saturday, October 11, 2003

*  Recent Topics

*  Fog Creek Home