24

I'm primarily a c#/.NET programmer, but the hobby project that I am about to take on will involve coding in c.

I know that there is a ton of c code out there, but its very hard to separate a well structured c project from a badly structured project if you did not "grow up" with the language. Can anyone point me toward a newer, non trivial (but not enormous) project that is considered a good example of how to structure c code?

Edit

If you cannot find a project, feel free to discuss in words how you think a c project should be structured.

Community
  • 1
  • 1
Giovanni Galbo
  • 12,963
  • 13
  • 59
  • 78

8 Answers8

16

Dave Hanson's C Interfaces and Implementations is a very well-crafted library of about 7000 lines of code. The book is great if you can afford it.

The implementation of the Lua programming language and libraries is about 17,000 lines of C code and is very well done but quite sophisticated in places—compilers and garbage collectors are not so easy to understand without background in those fields. But the code is beautifully engineered.

Git is over 125,000 lines of code. I can't recommend that anyone study it as an example of how to engineer C code. Just the design and public interfaces, let alone the implementations, are hard to understand—which is why there are so many git tutorials.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • 1
    +1 for Lua. I was pleasantly surprised when I took a look under the hood to see if it was going to be acceptable performance and quality wise for an embedded software project I was on. – patros Aug 20 '09 at 03:59
  • 1
    Git has consistent style and structure so it is good material to learn. You don't have to ready every line of it! – Piotr Czapla Aug 20 '09 at 10:00
  • @Norman looks like the book is available on Safari. I must have skipped it over because it is 13 years old (assumed it wouldn't be "modern code"), but I'll check it out on your recommendation; thanks! – Giovanni Galbo Aug 20 '09 at 11:38
  • 1
    125,000 lines of GIT versus 17,000 lines of Lua doesn't make much diference in this case, since he will not (probably) read every line... – Alceu Costa Aug 20 '09 at 13:30
  • 2
    @git advocates: How will the OP know where to get started? Any thoughts? – Norman Ramsey Aug 20 '09 at 17:01
6

Check out git source code: http://github.com/git/git/tree/master

Piotr Czapla
  • 25,734
  • 24
  • 99
  • 122
6

I consider Steve Dekorte's IO language implementation to be a good example for clean and pragmatic modern-day C.

earl
  • 40,327
  • 6
  • 58
  • 59
3

This is completely anecdotal, but I've heard that the SQLite project is considered good code.

Andy Gaskell
  • 31,495
  • 6
  • 74
  • 83
1

How about:

"The C Programming Language", 2nd edition, Kernighan and Ritchie

Answers to Exercises

http://users.powernet.co.uk/eton/kandr2/

1

The following book (with code examples) could ease your paradigm shift a bit:

Stephen Kochan

Programming in C, Third Edition

http://www.kochan-wood.com

1

C: A reference Manual By Harbison and Steele

C: A Reference Manual contains a variety of example code usage, with implementations centric to Standard C; A definite document to look into, perhaps along with the C Std.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
amaterasu
  • 1,040
  • 7
  • 8
1

The SVN project is written entirely in C and it is well maintained: consistently styled, good comments, low code smell. I recommend perusing it.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222