Difference between revisions of "C Coding Standard"
(5 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
[[Category:Coding Standards]] | [[Category:Coding Standards]] | ||
+ | ==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 | ||
+ | *::<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). | ||
+ | * Use <c>"%f"</c> instead of <c>"%lf"</c> specifier in <c>printf</c> and family to print <c>double</c> (see bug#13872). This does not affect <c>scanf</c> and family. | ||
+ | ==Style== | ||
* Always use curly braces, even when there is only one instruction. Instead of | * Always use curly braces, even when there is only one instruction. Instead of | ||
<c>if (a) do_something(); | <c>if (a) do_something(); | ||
Line 32: | Line 44: | ||
} | } | ||
</c> | </c> | ||
− | |||
− |
Latest revision as of 00:45, 2 August 2013
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).
- Use
"%f"
instead of"%lf"
specifier inprintf
and family to printdouble
(see bug#13872). This does not affectscanf
and family.
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 { ... }