Coding Style for C Programming

A well-written computer program is more than just a debugged and tested program, it should also be readable to yourself and to others. Doing this requires a certain amount of discipline, but fortunately this discipline is not difficult to acquire. The purpose of paying attention to coding style is to make reading and fixing code easier to do, and the effort of writing using a good style makes it more likely your program will be correct in the first place.

Computer code doesn’t need to look pretty, it just needs to look simple and easy to understand. A lot of students spend a lot of effort trying to make their code look pretty, but creativity in typography is often better redirected at making the code actually work well.

This set of notes aims to describe a simple, neat programming style which you should try to follow in your own coding for this course. You don’t have to follow this style, as long as the other people in your programming team follow a common style as you; otherwise the code handed in by your team will be mismatched and more difficult to follow.

The C Programming Language (2nd edition) by Brian Kernighan and Dennis Ritchie, defines a neat coding style which we will follow here. We’ll call this the Kernighan and Ritchie (or K&R) style.

The rest of this document will discuss the rules of this coding style, and the reasons why those rules help in writing code which is correct.

Links: http://www.cs.usyd.edu.au/~scilect/tpop/handouts/Style.htm