Free Software Stuff

Coding style for C programs

This document describes the coding style I try to enforce in C source code. This is, mostly, “K&R style” but with a few variations.

Indentation

Function definitions and prototypes

Example:

static int
foo_bar(int         frobnicate,
        const char *src,
        const char *dst,
        double      foo_ratio,
        int         bar_type)
{
    function_with_an_absurdly_long_name(frobnicate, (char *)src, dst,
                                        foo_ratio, bar_type);
}

Control flow

Examples:

if ( foo ) {
    /* ... */
}
else if ( ! bar ) {
    /* ... */
}

do {
    /* ... */
} while ( foo && ! (bar < 0 && frob != 2) );

Using GNU Indent

The following options for GNU Indent can be used to approximate the style described above (except the spaces around the conditional expression inside the parens—the -prs option makes no distinction between the outermost parens and the nested parens):

-kr -blf -nce -l78 -psk -sc -nut -ss

Naming convention