top logo menu kitty mascot
AROS.ORG Forum Software Bounties

AROS C/C Style Guide (KR-4)

Scope

This guide applies to all .c and .h files. If there is a conflict between local style and this guide, this guide wins.

1) Indentation and whitespace

Indentation

  • Indent with 4 spaces per level.
  • Do not use tabs for indentation.

Example:

if (cond) {
    do_work();
}

Operator spacing

  • Put spaces around binary operators.

Examples:

a = b + c;
mask = (x << 2) | y;
ptr = base + offset;
  • Unary operators remain tight where idiomatic.

Examples:

i++;
--j;
p = &obj;
q = *p;

Parentheses spacing

  • Do not add spaces immediately inside parentheses.

Examples:

if (a == b)             /* not: if ( a == b ) */
func(x, y);             /* not: func( x, y ) */

2) Braces and block layout

K&R brace style

  • Opening brace on the same line as the control statement or signature.
  • Closing brace on its own line.

Example:

static int foo(int x)
{
    if (x > 0) {
        return x;
    }

    return 0;
}

else placement

  • else follows the closing brace on the same line.

Example:

if (ok) {
    do_a();
} else {
    do_b();
}

3) do { } while (); formatting

Example:

do {
    work();
} while (cond);

4) Pointer alignment

Examples:

char *p;
const struct Foo *foo;
struct Node *node;
void (*fn)(int x);

Example:

char *a, *b;

5) Line length

6) Line endings

7) Statements and blank lines

Preferred:

if (error) {
    return FAIL;
}

8) Headers and includes

9) Formatting workflow