Use a good C style to format your source code files

A good C style of of the source code you write represents you. You will be judged by other people by the way your source code looks. Always try to write beautiful code.

There are a few possible good C styles. We recommend one that is well documented and comes with a Perl script for checking. The script does not catch everything but is good enough. However, in order to use it properly, you need to start with following the C style after reading the document below, then check the resulting code with the script. It will not work well if you start with non-conforming code and keep fixing the warnings until the script no longer reports anything. In that case, the resulting code may still be pretty bad style-wise.

The recommended C style is used for the Oracle Solaris operating system source code and is explained in a short (~20 pages) nicely formatted document: C Style and Coding Standards for SunOS.

The cstyle.pl for verification is provided, and it works like this:

$ chmod u+x cstyle.pl
$ ./cstyle.pl src/exams/parallel-udp/*.c
src/exams/parallel-udp/client.c: 12: missing space between keyword and paren
src/exams/parallel-udp/client.c: 13: missing space between keyword and paren
src/exams/parallel-udp/client.c: 25: line > 80 characters
src/exams/parallel-udp/server.c: 14: missing space between keyword and paren
src/exams/parallel-udp/server.c: 15: missing space between keyword and paren
src/exams/parallel-udp/server.c: 25: missing space between keyword and paren
src/exams/parallel-udp/server.c: 45: line > 80 characters
src/exams/parallel-udp/server.c: 50: line > 80 characters
src/exams/parallel-udp/server.c: 56: line > 80 characters

Another very good C style is what the OpenBSD team uses, it is very similar to the Solaris C style, there are some minor differencies. Apparently they do not provide a style checker. See the style(9) manual page for more information.


Last changed: Wed Oct 10 2018 11:23:57 GMT+0200 (CEST)