Difference between revisions of "C Coding Standard"

m (Language consideration)
m (Language consideration: Added a note on function prototypes without arguments)
Line 6: Line 6:
 
* Never use '''return''' if this is not the last instruction of a routine.
 
* Never use '''return''' if this is not the last instruction of a routine.
 
* Use '''break''' only to separate branches of a switch statement.
 
* Use '''break''' only to separate branches of a switch statement.
 +
*A declaration
 +
*::<c>extern void foo();</c>
 +
*:needs to be
 +
*::<c>extern void foo(void);</c>
 +
*:Otherwise it is not considered a prototype (for compatability with K&R) (see bug#13870).
  
 
==Style==
 
==Style==

Revision as of 07:13, 18 August 2009

Construction.png Not Ready for Review: This Page is Under Development!

Language consideration

  • Avoid using assignment inside an expression.
  • Never use goto.
  • Never use return if this is not the last instruction of a routine.
  • Use break only to separate branches of a switch statement.
  • A declaration
    extern void foo();
    needs to be
    extern void foo(void);
    Otherwise it is not considered a prototype (for compatability with K&R) (see bug#13870).

Style

  • Always use curly braces, even when there is only one instruction. Instead of
if (a) do_something();

write

if (a) {
	do_something();
}
  • Place opening brace at the same line as the condition as in
while (expr) {
	...
}
for (;;) {
	...
}

unless the expression is very long as in

if
	(very_long_expression)
{
	...
}
  • Surround else with braces as in
if (expr) {
	...
} else {
	...
}