Difference between revisions of "C Coding Standard"

 
(Added C standard guidelines)
Line 1: Line 1:
 
[[Category:Coding Standards]]
 
[[Category:Coding Standards]]
 +
 +
*  Always use curly braces, even when there is only one instruction. Instead of
 +
<c>if (a) do_something();
 +
</c>
 +
write
 +
<c>if (a) {
 +
do_something();
 +
}</c>
 +
 +
* Place opening brace at the same line as the condition as in
 +
<c>while (expr) {
 +
}
 +
for (;;) {
 +
}
 +
</c>
 +
unless the expression is very long as in
 +
<c>if
 +
(very_long_expression)
 +
{
 +
...
 +
}
 +
</c>
 +
 +
* Surround else with braces as in
 +
<c>if (expr) {
 +
...
 +
} else {
 +
...
 +
}
 +
</c>
 +
 +
* Avoid using assignment inside an expression.

Revision as of 22:12, 20 July 2009


  • 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 {
	...
}
  • Avoid using assignment inside an expression.