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
- Indentation level is 4 spaces.
- Use either spaces only, or a mix of tabs and spaces (not quite decided
yet). If using tabs and spaces, always use 8-stop tabs (that is, never
change the editor’s setting of what a tab is).
Function definitions and prototypes
- The return type of a function is on its own line before the function
name and the list of arguments, both in the prototype and in the
definition.
- No spaces between the function name and the opening paren.
- No spaces between the parens and the arguments inside.
- The braces enclosing the function body are on their own line, on the
first column.
- Arguments in prototypes are named.
- Arguments are all on the same line when possible (if the function name
and the arguments list fit on a 78-characters line). Otherwise:
- each argument is on its own line;
- the first argument is on the same line than the function name;
- the last argument is on the same line than the closing paren;
- arguments are aligned on the column next to the opening paren;
- argument names are aligned on the closest common column after the
types, leaving one intervening space;
- pointer marks are “dangling” on the left of argument names.
- When calling a function, if the argument list does not fit in a
single line, dispatch arguments only on as many lines as necessary (do
not put each argument on its own line).
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
- No braces around single-statement conditional blocks.
- The opening brace of a conditional block is on the same line than the
associated
if
, for
, while
, do
, or else
keyword, and is
preceded by a space.
- The closing brace of a conditional block is on its own line, aligned
with the associated control keyword.
- A
else
keyword is on the line following the associated if
-block.
- In a
do-while
loop, the while
keyword is on the same line than the
closing brace of the do
-block.
- There is one space between all control flow keywords and the following
opening paren.
- There is a space before and after the conditional expression inside
the parens. But there is no spaces inside nested parens.
- There is one space after the
!
operator.
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
- Function, variable, and type names are in lowercase with words
separated by underscores.
- Preprocessor symbols and enum values are in uppercase, again with
underscores.
- Aliased types are suffixed with
_t
.