TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Examples of especially well-written C code?

21 点作者 dlowe超过 14 年前
In light of the recent OpenSSL rant (http://news.ycombinator.com/item?id=1680337): what are some open source projects written in C which are particularly well-written?<p>I can give one example: I was very pleasantly surprised by the experience of cold-reading the BerkeleyDB source (trying to explain some very odd observed behavior) a few years ago. The code was laid out in a sensible way, such that I could find what I needed reasonably quickly. It was generally clear and straightforward to read, and the few places with "tricky" code were very nicely commented -- including the source of the odd behavior I was hunting for.<p>Are there other examples of great, readable C code out there?

11 条评论

nkurz超过 14 年前
In the past, I've found the code for SQLite to be brilliantly clear: <a href="http://sqlite.org" rel="nofollow">http://sqlite.org</a><p>Going further, I'd suggest that most large successful projects in C have clear code that is worthy of study. It's some corollary of the anthropic principle --- if the code wasn't clear, it would never have survived to become successful.<p>This may only apply to the core of these projects (written by experts) than to the periphery (written by learners). But you might be surprised by how readable the central code is for Apache, Linux, Glibc, Mysql, Perl, Python, etc.
评论 #1681485 未加载
kqr2超过 14 年前
I would recommend David R. Hanson's book <i>C Interfaces and Implementations</i>. It is an extremely well documented library and shows you how to design APIs properly.<p><a href="http://www.amazon.com/Interfaces-Implementations-Techniques-Creating-Reusable/dp/0201498413/" rel="nofollow">http://www.amazon.com/Interfaces-Implementations-Techniques-...</a>
评论 #1705332 未加载
评论 #1681816 未加载
tolini超过 14 年前
Do you mean something like this?<p><pre><code> main(l ,a,n,d)char**a;{ for(d=atoi(a[1])/10*80- atoi(a[2])/5-596;n="@NKA\ CLCCGZAAQBEAADAFaISADJABBA^\ SNLGAQABDAXIMBAACTBATAHDBAN\ ZcEMMCCCCAAhEIJFAEAAABAfHJE\ TBdFLDAANEfDNBPHdBcBBBEA_AL\ H E L L O, W O R L D! " [l++-3];)for(;n--&#62;64;) putchar(!d+++33^ l&#38;1);} </code></pre> (source: <a href="http://www.ioccc.org/years.html#1992_westley" rel="nofollow">http://www.ioccc.org/years.html#1992_westley</a>)
neopallium超过 14 年前
The Lua interpreter (pure ANSI C), both the core VM and the public C API for modules are well written. <a href="http://www.lua.org/" rel="nofollow">http://www.lua.org/</a>
acqq超过 14 年前
In most nontrivial projects, C is used to solve problems that are worse being solved with other platforms: targeting the biggest possible platform independence but caring about performance. So you can't compare the "beauty" of something that is supposed to run on my 50 USD router with 32 MB with something that has not much to do (measuring CPU and memory use) and has the luxury to be compiled only by a few compilers.<p>Another problem is that a lot projects "grow" without any vision. Once you understand that, you'll find that most of the successful C projects didn't have much more choice, more because of the history of them than anything else.<p>However when you have the full control of the platform and have a small numbers of the developers with a good taste (you control the OS and the compiler too) you can maintain "beauty" consistently. In this regard, I suggest you to enjoy Plan 9 sources!<p><a href="http://plan9.bell-labs.com/wiki/plan9/Sources_repository/index.html" rel="nofollow">http://plan9.bell-labs.com/wiki/plan9/Sources_repository/ind...</a>
cperciva超过 14 年前
I've been told that the Tarsnap source code is particularly good. :-)
评论 #1681935 未加载
iampims超过 14 年前
Redis is wonderfully well written.<p><a href="http://github.com/antirez/redis" rel="nofollow">http://github.com/antirez/redis</a>
评论 #1681752 未加载
jmspring超过 14 年前
I'm a fan of parts of the BSD kernel myself. My first exposure was through the book "The Design and Implementation of the FreeBSD Operating System". Then again, dorking around in kernels and the like hold some interest for me.
评论 #1682977 未加载
j_baker超过 14 年前
Python's C code is pretty well written. Plus it can be useful to know about if you actually write Python.
lzimm超过 14 年前
i really liked the redis codebase. not sure if its changed much since it became a much bigger project, but back when it was mainly @antirez, it was some of the cleanest code i've ever read.
inf_variance超过 14 年前
PostgreSQL and the Linux kernel sources make great reading materials.