Following text describes Acrios unified coding style for C/C++ code. Herein described method
uses AStyle
utility for automatic formatting.
We indent using four spaces.
No trailing whitespace is allowed.
We use so called Allman's style:
int Foo(bool isBar)
{
if (isBar)
{
bar();
return 1;
}
else
{
return 0;
}
do
{
}
while (1);
}
Alternatively, Kernighan and Ritchie (K&R) style is allowed:
int Foo(bool isBar)
{
if (isBar) {
bar();
return 1;
} else {
return 0;
}
do {
} while (1);
}
Continuation lines following lines with opening parenthesis are indented.
void Foo(bool bar1,
bool bar2)
{
isLongFunction(bar1,
bar2);
isLongVariable = foo1
|| foo2;
}
becomes:
void Foo(bool bar1,
bool bar2)
{
isLongFunction(bar1,
bar2);
isLongVariable = foo1
|| foo2;
}
We indent preprocessor blocks:
#define Is_Bar(arg,a,b) \
(Is_Foo((arg), (a)) \
|| Is_Foo((arg), (b)))
becomes:
#define Is_Bar(arg,a,b) \
(Is_Foo((arg), (a)) \
|| Is_Foo((arg), (b)))
Arithmetic operators are padded with single space. Space is also inserted between a
header (if,while,for) and following paren
if(foo==2)
{
a=bar((b-c)*a,d--);
}
becomes:
if (foo == 2)
{
a = bar((b - c) * a, d--);
}
Body of each condition has to be wraped in braces.
if (a > 4)
{
printf("Larger than 4");
}
We use Linux line ending (LF).
AStyle formatter is available for most platforms. On Ubuntu, install with:
sudo apt install astyle
All rules mentioned above are summarized in AStyle configuration file:
To format single file using astyle:
astyle --options=".astylerc" file.c
To format all .c and .h files in a src
directory:
astyle --options=".astylerc" src/*.{c,h}
To recursively format all .c and .h files in a src
directory:
astyle --options=".astylerc" -R src/*.{c,h}
AStyle homepage
AStyle documentation