tags:

views:

726

answers:

9

Following the "what's the best use for C" question.

The Linux kernel seems to be a famous and very well thought of C program. But is it a good example of mainstream "best-practice" C?

+9  A: 

Yes, it is.

If you don't want to get overwhelmed with the details of the kernel, you can start with some of the Linux utility functions written in C. See the answers to this question for details on how to get the source.

Another good place to read C source code is in C's very own Standard Library.

Finally, one more recent C project that I'm hearing good things about is Git.

Bill the Lizard
I'm not sure why this was downvoted. It is a yes or no question.
Bill the Lizard
I'm not sure either. +1, good answer.
unforgiven3
Standard C Library seems to be a good starting point to me too
Gustavo Rubio
Plauger wrote a book with really good examples from the C standard library too. http://www.amazon.com/Standard-C-Library-P-J-Plauger/dp/0131315099
Bill the Lizard
+10  A: 

What do you mean by good example? I believe the Linux kernel is a very well written kernel in C with some problems, many of which are being cleaned up. However, as far as using it as an example of how to write general C code, I don't think it quite fits. Reading and understanding the kernel will teach you a lot about C. But what you won't find in the kernel is any libraries to speak of. The standard libraries are unavailable. So, for instance, you will see "printk" instead of "printf" and so forth. There is also some dark magic. The kernel is a very special case. I would not use it to find best practices, say, if you were writing some general application like a file manager or a database.

BobbyShaftoe
+2  A: 

If you are asking from the perspective of learning C, then it might be too overwhelming for a start. As far as usage of C is concerned, it is best suited for applications requiring portability and performance. It is commonly used for system level programming. But, it does so at the expense of abstraction. Therefore, you have to do the bulk of coding yourself unlike a language like Java or even C++. Ideally, your application type should decide the choice of language, and with operating systems C is the way to go.

One way to go about learning about the Linux kernel is starting from a very old linux kernel version. This would contain the meat and hide the additional complexity present in the current kernel. This should be sufficient in understanding the basics.

Epitaph
I think the question was more about best practices for *using* C.
BobbyShaftoe
+1  A: 

Whether it is "best practice" depends on who you are asking. There are a lot of low level C programs written on Windows platform too, but if you take a look at them (e.g., device driver examples from WINDDK), you will realize that Microsoft has successfully invented another language that they call C :-) But still, those should probably be considered as "best practice" if you ever want to develop device drivers on Windows.

PolyThinker
+3  A: 

I would suggest looking at the gnu utilities (e.g. the source code for standard unix utilities such as 'ls' and 'awk') as they are going to be easier to understand and more likely going to be written in a more clean manner (the kernel has different performance needs and compatibility concerns than other types of software).

Travis
A: 

One excellent place is to look at the code of some open source compiler in use. You need excellent data structure and pointer skills to write a compiler.

Perpetualcoder
Sure, as long as you ignore GCC. Which isn't particularly great.
Simon
+11  A: 

I'm prepared to get dinged for this, but the Linux kernel is a student project that got out of hand. More seriously, what the Linux kernel does well from a programming point of view is lay down a very rigid set of design and coding guidelines that make it possible for a very large number of people to contribute while still having everything fit together.

People I trust who have made operating systems their life's work tell me that the BSD kernel is a much better example of good C programming and good OS design. I know enough of the professors at Berkeley that I'm not surprised by this claim.

More broadly, OS kernels are highly specialized things---there's nothing mainstream about a kernel. If you want examples of good C practice you might try Kernighan and Pike's book on The Practice of Programming. Or if you want to study a real system, the implementation of Lua is small enough that you can read it all, extremely well engineered, and incredibly portable---runs on any platform with an ANSI C compiler.

Apologies for the subjective and argumentative reply :(

Norman Ramsey
I would not find your suggestion to be surprising at all. I think that Linux's success is more sociological than technical.
Travis
+1  A: 

For mainstream programs? No. *nix kernel code is generally not in userland, and as such has different conventions and requirements than most "mainstream" programming. It is, however, some of the best and highest-quality C code you can study to learn from. It has its weaknesses, and it's not entirely well-written, but for the most part it is very effective, clean code.

Max
A: 

I find the kernel to be a very good example on how to write modularized code. Despite being C, it is somewhat object oriented.

Maister
Although the way it achieves 'object-oriented' is overwhelmingly complex, and definitely not a place to learn from for starters. I'm guessing a compiler would be a much better place to learn 'object-oriented' in C.
Vanwaril