Ask HN: Good C codebases to read?
8 comments
One of the very good sources of knowledge is the FreeBSD code base. You can check it out from Subversion, and have a complete operating system in the .c and .h form. This is actually how I gained the proficiency in C. It exposes you to industry-grade C (e.g.: you'll learn about bits which are hard in C but are being used actively) Also if you ever get interested in operating systems, FreeBSD gives you a very nice transition, since both programs and the kernel are in the same repo.
To check out the FreeBSD source code, you can do:
svn co https://svn.freebsd.org/base/head
And if you want to know how 'cat' is implemented, go to bin/cat/.
Same for 'ls', 'find' etc..
Other than that, early source code of UNIX gurus is very good too. Check out the Plan 9 project from Bell Labs. It has a very clean code too:
http://plan9.bell-labs.com/sources/plan9/sys/src/
To check out the FreeBSD source code, you can do:
svn co https://svn.freebsd.org/base/head
And if you want to know how 'cat' is implemented, go to bin/cat/.
Same for 'ls', 'find' etc..
Other than that, early source code of UNIX gurus is very good too. Check out the Plan 9 project from Bell Labs. It has a very clean code too:
http://plan9.bell-labs.com/sources/plan9/sys/src/
There is also an official mirror on Github. https://github.com/freebsd/freebsd
Both Lua and LuaJit.
https://github.com/LuaDist/lua
https://github.com/LuaDist/luajit
https://github.com/LuaDist/lua
https://github.com/LuaDist/luajit
Lua's C code base is fairly complex. I don't recommend it to a people willing to study C more. When I attempted to embed Lua into the FreeBSD's loader I expected it to be much easier. But when things went wrong and I had to jump and understand Lua internals, it too a while.
Redis' src is very clean and easy to dive into.
https://github.com/antirez/redis
https://github.com/antirez/redis
Any good C++ codebases?
gcc, postfix, fossil, a lot of major projects have good C code.
Fossil is a high quality codebase but a little odd in many respects. For example, the build process runs three custom preprocessors to generate header files: http://fossil-scm.org/index.html/doc/74836bc8ae09edd152d8/ww...
For a beginner looking for idiomatic C code I wouldn't really recommend it as something to study.
For a beginner looking for idiomatic C code I wouldn't really recommend it as something to study.
gcc is a good example of terrible C code.
I think the last time this came up sqlite was recommended
I find https://github.com/antirez/redis one of the best C codebase to read.
Inspired by this thread: https://news.ycombinator.com/item?id=9896369