News for hackers. Stuff that matters.

Ask HN: Interesting (small) bits of C or C++ code

Hey everyone!

I'm looking for interesting bits of C or C++ code, smaller programs or functions. That I can read and learn from.

Not looking for hello world examples, more complex. An example of "Hey thats neat" would be donut.c (http://www.a1k0n.net/2011/07/20/donut-math.html)

Anybody else know of cool little gems like this?

( 6 comments | 10 points | on HN )
Re:Ask HN: Interesting (small) bits of C or C++ code by Someone on Friday October 12, 2012 @04:27PM (#4646690)
If you want to learn C http://www.ioccc.org/ gives some nice examples.

If, on the other hand, you want to learn programming in C, do not use IOCCC sources as something to aim for :-). I haven't read any of it, but I suspect http://pdos.csail.mit.edu/6.828/2011/xv6.html will contain a wide complexity range of code.

Re:Ask HN: Interesting (small) bits of C or C++ code by jfaucett on Friday October 12, 2012 @02:24PM (#4646247)
coreutils is great if you're looking to buff up your c skills. http://www.gnu.org/software/coreutils/, just was looking over remove_suffix inside basename.c today actually.
Re:Ask HN: Interesting (small) bits of C or C++ code by quadfour on Friday October 12, 2012 @11:57AM (#4645573)
there is a reddit dedicated to small snippets of code called tinycode, but it does not focus in any particular language. Github maybe your best option.
Re:Ask HN: Interesting (small) bits of C or C++ code by thomasbk on Friday October 12, 2012 @05:30PM (#4646934)
A little-known operator in C++, the "goes to"-operator:

int i=4;

while(i --> 0) { cout << i; }

Re:Ask HN: Interesting (small) bits of C or C++ code by dysoco on Friday October 12, 2012 @08:20PM (#4647429)
Actually that's just decrementing and comparing to 0: while((i--) > 0)
Re:Ask HN: Interesting (small) bits of C or C++ code by athesyn on Friday October 12, 2012 @11:18AM (#4645393)
Github.

I've found cool/interesting functions (and even one-liners) by browsing around randomly.