clc-wiki:Code style guidelines [primary]
aliases: Code Style Guidelines (for this wiki) | Clc:Code Style Guidelines | clc-wiki:Code Style Guidelines
These recommendations have been moved to Project:Policies#C_code. This page is reserved for a partial explanation of K&R formatting for those unfamiliar with it.
The basics of K&R formatting
- a single space follows a keyword - if, while, etc - but not a function name
- a single space follows and precedes all binary operators (with the exception of
.which should not be preceded or followed by whitespace) and each token of the ternary operator i.e.
x ? y : zrather than
x += yrather than
somestr -> somemember
- exceptions to the above are constructs like
x >> (p+1+n)which should not have spaces around the pluses
- a single space follows a comma and follows each semicolon within a
forloop's control statement.
*when used as the dereference operator or as part of a pointer declaration, associates with the declared object/dereferenced expression i.e.
char *p;rather than
- for functions, the opening brace occurs at the start of a new line; likewise the closing brace
- for all other blocks, the opening brace occurs at the end of the statement that opens the block, preceded by a single space; the closing brace occurs on a newline indented to the same level as the block's opening statement. i.e.