Coding Style
Indent Style in C
Use same indent style with existing code. One indentation is made by 4 spaces. No tabs are used because their width depends on environments.
- indentation by 4 spaces.
not:
if (a < b) {
return a;
}
but:
if (a < b) {
return a;
}
- space after if, while and for.
- braces on if, while and for line.
- no space on inner side of parentheses.
- spaces on both sides of binary operators(<, >, ==, !=, =, +=, etc.).
- no space between unary operators(++, --, !, etc.) and their operands.
not:
if( a<b )
{
return a;
}
else
{
a ++;
return b;
}
but:
if (a < b) {
return a;
} else {
a++;
return b;
}
If you are using emacs, add the following code to your .emacs.
(c-add-style
"ruby"
'("bsd"
(c-basic-offset . 4)
(tab-width . 4)
(c-offsets-alist
(case-label . 0)
(label . 0)
(statement-case-intro . 4))))
It help you to indent the code in emacs because all C source files include the following line.
/* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
Keyword(s):
References:[SideMenu]