Skip to content
Pedro A. Hortas edited this page Apr 16, 2015 · 17 revisions

Indentation

  • Do not indent more than 4 times.
  • Refer to the following subsections to see the indentation style used on the project.

Functions

/* A function with a few arguments */
int func_few_args(int arg1, char *arg2, const char *arg3) {
       ...
       /* All good */
       return 0;
}

/* A function with lots of arguments */
int func_many_args(
        int arg1,
        int arg2,
        char *arg3,
        const char *arg4,
        long arg5,
        unsigned long long arg6)
{
        ...
        /* All good */
        return 0;
}

Switch / Case

switch (opt) {
        case ONE: {
            ...
        }; break;
        case TWO: {
            ...
        }; break;
        default: {
            ...
        }; break;
 }

If / Else

if (cond) {
        ...
} else {
        ...
}

For / While

while (cond) {
        ...
}

for ( ; cond; ) {
        ...
}

Increment / Decrement

i ++;
-- i;

Endianess

  • Always use stdint.h types (uint16_t, uint32_t, etc) on variables that can possibly be transmitted to other systems.
  • Always use htons(3), htonl(3), ntohs(3) and ntohl(3) on uint16_t and uint32_t type variables that are intended to be transmitted to other systems.

Portability

C99 / C11

Grant that your code conforms to C99 and/or C11.

POSIX

  • Grant that your code conforms to, at least, POSIX.1-2001. If, for some good reason, an optimization non-POSIX compliant is used, that portion of code must be optional (and determined in compile time whether it'll be used or not).

Clone this wiki locally