views:

1260

answers:

13

Everyone knows that C is hard to program in, provides you with almost no useful abstractions, and is even dangerous. However, given enough design and discipline, it's possible to write clean, portable, modular code. What are some good examples of this? I'm looking for real world applications or other open source projects that are reasonably large yet manage to stay readable and expressive on all levels.

If you can, cite an article or documentation so others can become familiar with it.

+1  A: 

Simon Tatham's Portable Puzzle Collection has a frontend/backend architecture that allows cross-platform puzzles to talk to platform-specific user interfaces via a well-defined API. Adding a puzzle consists of writing your logic to a certain interface, and porting the entire collection consists of implementing main and the drawing API. The developer documentation describes this in depth.

jleedev
+9  A: 

Linux.

While I'm tempted to give a shout-out to my own project, it's rather useless unless you're interested in esoteric programming, and I'm still working on fixing it up a bit, so it may not yet be the best example of safe, portable, module code. So in the meantime, Linux.

EDIT: Linux may be a bit hard on a newcomer to C (or just to the Linux codebase). Perl and Python (and maybe Ruby) - the programs that interpret the Perl/Python/Ruby languages - are also good examples of good, solid, safe C code, and may either be a little easier to get into or a little harder to get into, depending on where you come from. You can check up on Perldoc's perlguts page to (try to) see into Perl's C internals. Not being a Python or Ruby programmer, I don't know where to go for those, but I imagine Google can help a lot.

Chris Lutz
Perlguts is very interesting!
jleedev
how comes i haven't noticed your answer before i posted my linux answer? well, now i noticed i was too late posting my linux answer. noticed it only when some guy apparently was thinking "haha litb steals the other ones answer" (i *really* didn't notice yours!). +1 from me of course.
Johannes Schaub - litb
It's okay, litb. You had the picture of Tux to make it all worth it.
Chris Lutz
+2  A: 

Pick a domain you like (graphics, video, machine learning, numerics, networking, operating systems, scientific, etc), and then browse around for a popular open source project in C (SourceForge is a good place to start). The keen interest helps when you're faced with a steep learning curve in familiarizing yourself with a project.

Zach Scrivena
A: 

99 bottles of beer is a start to be familiar with the C syntax.

LicenseQ
bottles vs bollles?
SAMills
not a real world application!!!! -1
hasen j
depends what is real :-)
LicenseQ
@LicenseQ: `real` is Fortran - in C, use `float` or `double` ;)
Christoph
+10  A: 

What about Minix?

About 12,000 lines of good C source code, written for educational purposes: the kernel, the memory manager, and a file system.

I think it's a great source to learn more about C and Operating Systems!

CMS
I'm taking Operating Systems right now. Thanks for the suggestion about Minix.
jleedev
You're welcome, enjoy your course!!
CMS
+2  A: 

The windows kernel and much of the related components are written in C (still).

Foredecker
Too bad we can't look at it and see how well it's written. Not to gloat or be an open-source-militant, but open-source programs are better to learn from because you can see the example code in practical actual use right there.
Chris Lutz
It is very well written - the kernel team has a very well defined 'style'. Large portions are very much "old school". No gloating necessary - open source is very groovy. There is a lot of respect at MSFT for Linux, Apache etc. etc.
Foredecker
+1  A: 

Openbox window manager, it's a great lightweight window manager all it's direct competitors are written in C++ and it is a rewrite of a wm that was written in C++.

The Doom engine, could be worth a look I haven't dived into it yet but considering it was a commercial program I would assume it is well documented and readable.

Doom Source

Openbox Website

Aaron C
"considering it was a commercial program I would assume it is well documented and readable." -- Ah, you're still in for *that* shock. Look forward to it :)
Jonas Kölker
+1  A: 

Python is written in C. That probably meets your criteria.

Baltimark
Already mentioned it. Though for some reason Python is also (partially) written in Java by some people. No disrespect to Java, but this makes no sense to me.
Chris Lutz
Maybe if you see it as accessor to Java libraries it makes much more sense. The code base for Java is probably quite large ... Regards
Friedrich
+1  A: 

Well currently 99 % of all Operating Systems have C as "base". There were tries like BeOS which just vanished. Nearly every scripting language is written in C. I especially must point out the sources of Tcl and Io. That is just "great" written C. The points with kernels is that they are extremly large, so understanding it it extremly much work. It's a bit easier with the scripting lanuages. If you are just looking for large C code sizes. Then you can also check out MySQL, PostgresSQL and the like they are also written in C and they must be quite mature because millions of people trust them their data....

Friedrich
Actually, BeOS is alive and well in the form of Haiku. http://www.haiku-os.org/
Software Monkey
Sorry, I did not know that. thanks for the link...
Friedrich
+2  A: 

Apache

The Apache HTTP Server Project is a collaborative software development effort aimed at creating a robust, commercial-grade, featureful, and freely-available source code implementation of an HTTP (Web) server. The project is jointly managed by a group of volunteers located around the world, using the Internet and the Web to communicate, plan, and develop the server and its related documentation. This project is part of the Apache Software Foundation. In addition, hundreds of users have contributed ideas, code, and documentation to the project.

Apache Portable Runtime.

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.

Paulius Maruška
A: 

Samba, clean and manageable code

dmityugov
+1  A: 

I can second Simon Tatham's puzzle collection. If you know how to generate instances of a puzzle, writing an implementation of that puzzle as part of the collection is actually quite easy (and rewarding).

Also, the architecture is sound and the code is well commented. Give it a try, it's fun :-)

Jonas Kölker
+1  A: 

The RTOS uC/OS-II (wiki) is written in C (aside from the task switcher which is platform-specific and almost always written in assembler since it messes with the stack). The code is well documented, and the book is essentially a textbook on RTOS implementation principles, taking uC/OS-II as an example.

The code is generally very readable, which is moderately surprising for a commercial product as alluded to in other comments. It is also a decent little operating system kernel if you have need of such things. I found it easy to port to my hardware, and have used it in several projects over the years.

RBerteig