C -like Language Formatting Rules
Stefan Ram, 1999–2003
Not everybody likes my style of formatting—in fact, most people don't. This is an attempt to write down my rules.
1. Meta Rules
1.a.) Rules later in this text have higher priority. This rule is often used in the following text in order to solve contradictions.
2. Spaces, Operators, and the Semicolon
2.a.) A space follows postfix unary operators. (This rule is rarely used, because it usually is overridden by rules with higher priority.)
2.b.) There are spaces around binary operators. There are spaces around the parts of ternary operators.
2.c.) There are no spaces around the binary operators "." and "->".
2.d.) The comma operator is followed, but not preceded, by a space. The semicolon is formatted just like the comma.
2.e.) A space precedes, but does not follow prefix unary operators. No space precedes postfix unary operators.
Examples
preferred instead of
x = y + z; x=y+z;
x = a * b + c++; x = a*b + c ++;
x = &y + *z + !v; x = & y + * z + ! v;
x = y.z + z->a; x = y . z + z -> a;
x = y + z, k; d = b; x = y + z,k;d = b;
a ? b : c; a?b:c;
3. Spaces, Parentheses, Braces, and Brackets
A Parenthesis, a Brace, or a Bracket is called a delimiter here.
3.a.) A space is inserted at the inner side of parentheses, braces, and brackets.
3.b.) A space must not be inserted at the outer side of parentheses, braces, and brackets.
3.c) There are no spaces inside of otherwise empty parentheses, braces, or brackets.
Examples
preferred instead of
y = f( x ); y = f(x);
if( x ) if (x)
a = !( b ); b =( a ); a = !(b); b = (a);
t = v( g[ b ], h ); t = v (g[b] ,h);
y = q(); {}; y = q( ); { };
{ char x[], t[ 5 ]; } {char x[ ], t[5];}
3.d) An empty pair of a parentheses (brace or bracket) directly followed by a closing parenthesis, brace, or bracket must be separated from this directly following closing parenthesis (brace or bracket) or directly preceding opening parenthesis (brace or bracket) by a space.
Examples
preferred instead of
a =( f() ); a =( f());
redundant note 3.e.) The rule 3.b. forbidding spaces around parentheses and brackets is stronger than the rules 2. by 1.a., so that spaces around operators are omitted on the side of an operand in parentheses or in brackets.
Examples
preferred instead of
a =( b +( x * c )+ d( q )); a = ( b + ( x * c ) + d( q ) );
t = v( g[ b ], h ); t = v (g[b] ,h);
y = q(); {}; y = q( ); { };
{ char x[], t[ 5 ]; } {char x[ ], t[5];}
y =( f )( x ); y = ( f )( x );
redundant note 3.f.) The rule 3.b. forbidding spaces around parentheses is stronger than the rule 3.a. requiring a space inside parentheses by 1.a., so that some spaces are omitted in the case of nesting.
Examples
preferred instead of
t =( f( b )); a =( f( b ) );
p = x *( a + b[( 4 )])}; p = x *( a + b[ ( 4 ) ] ) }
y =( f )( x ); y = ( f )( x );
4. Spaces
4.a) Two adjacent spaces are replaced by a single space.
Examples
preferred instead of
a =f( x ); a =f( x );
{ if( x ) { if( x )
5. Indentation
5.a) An arbitrary number of spaces at the beginning of a line may be used to indent the line according to the following rules.
5.b) All characters have an indentation value of 0, when not specified otherwise.
5.c) An opening parenthesis, brace, or bracket has an indentation value of +2. A closing parenthesis, brace, or bracket has an indentation value of –2. All indentation values of a line are added to calculate the modification of the indentation for the following line with regard to the current line (in units of spaces).
5.d) All characters with an indentation value of x are followed by x –1 spaces, whenever x >1.
redundant note 5.e.) The rule 5.d. requiring a space is stronger than rule 3.b. forbidding a space by 1.a. So, when two opening delimiters directly follow each other, they are separated by a space.
note) The position of line breaks in the following examples is not always the best choice regarding readability, it just serves as an example to show how indentation would be applied given these positions of line breaks, not to recommend these choices for positions of line breaks.
Examples
preferred instead of
if( x ) if( x )
{ if( y ) {if( y )
{ while( a ) {while( a )
f( x ); }} f( x ); }}
while( cos( while( cos(
2 )){ 2 )){
f( 3[ i f( 3[ i
]); } ]); }
a =( ( b )); a =( ( b ) );
v = a + v = a +
b; b;
v =( a + v =( a +
b ); b );
{ { f( x ); }} {{ f( x ); }}
6. Strings and Comments
The rules from above do not apply to text contained in string literals or comments.
7. Miscellaneous
7.a.) The tab-character must not be used.
7.b.) Spaces may be omitted within macro definitions or macro invocations.
7.c.) When several possibilities are equivalent by the preceding rules, then the shorter one should be preferred.
8. Alignment
Spaces may be added in order to align similar elements from consecutive lines in pleasant ways.
Examples
preferred
{ if( ERROREXITCONDITION1 ){ running = 0; looping = 0; error = 1; }
else if( ERROREXITCONDITION2 ){ running = 0; looping = 0; error = 2; }
else if( EXITCONDITION1 ){ running = 0; looping = 0; result = 1; }
else if( EXITCONDITION2 ){ running = 0; looping = 0; result = 1; }}
instead of
{ if( ERROREXITCONDITION1 ){ running = 0; looping = 0; error = 1; }
else if( ERROREXITCONDITION2 ){ running = 0; looping = 0; error = 2; }
else if( EXITCONDITION1 ){ running = 0; looping = 0; result = 1; }
else if( EXITCONDITION2 ){ running = 0; looping = 0; result = 1; }}